Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 22 | static 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 | */ |
| 40 | static 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 | */ |
| 56 | static 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 | */ |
| 70 | static 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 | |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 81 | unsigned long smc_rtt_create(unsigned long rd_addr, |
| 82 | unsigned long rtt_addr, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 83 | 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 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 142 | if (s2tte_is_unassigned_empty(parent_s2tte)) { |
| 143 | s2tt_init_unassigned_empty(s2tt); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 144 | |
| 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 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 153 | } else if (s2tte_is_unassigned_ram(parent_s2tte)) { |
| 154 | s2tt_init_unassigned_ram(s2tt); |
| 155 | __granule_get(wi.g_llt); |
| 156 | |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 157 | } else if (s2tte_is_unassigned_ns(parent_s2tte)) { |
| 158 | s2tt_init_unassigned_ns(s2tt); |
| 159 | __granule_get(wi.g_llt); |
| 160 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 161 | } else if (s2tte_is_destroyed(parent_s2tte)) { |
| 162 | s2tt_init_destroyed(s2tt); |
| 163 | __granule_get(wi.g_llt); |
| 164 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 165 | } else if (s2tte_is_assigned_empty(parent_s2tte, level - 1L)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 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_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 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 184 | } else if (s2tte_is_assigned_ram(parent_s2tte, level - 1L)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 185 | 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 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 201 | s2tt_init_assigned_ram(s2tt, block_pa, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 202 | |
| 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 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 209 | } else if (s2tte_is_assigned_ns(parent_s2tte, level - 1L)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 210 | unsigned long block_pa; |
| 211 | |
| 212 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 213 | * We should observe parent assigned_ns s2tte only when |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 214 | * 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 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 226 | s2tt_init_assigned_ns(s2tt, block_pa, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 227 | |
| 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 | |
| 250 | out_unmap_table: |
| 251 | buffer_unmap(s2tt); |
| 252 | buffer_unmap(parent_s2tt); |
| 253 | out_unlock_llt: |
| 254 | granule_unlock(wi.g_llt); |
| 255 | granule_unlock(g_tbl); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | unsigned long smc_rtt_fold(unsigned long rtt_addr, |
| 260 | unsigned long rd_addr, |
| 261 | unsigned long map_addr, |
| 262 | unsigned long ulevel) |
| 263 | { |
| 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; |
| 271 | unsigned long ipa_bits; |
| 272 | unsigned long ret; |
| 273 | struct realm_s2_context s2_ctx; |
| 274 | int sl; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 275 | |
| 276 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 277 | if (g_rd == NULL) { |
| 278 | return RMI_ERROR_INPUT; |
| 279 | } |
| 280 | |
| 281 | rd = granule_map(g_rd, SLOT_RD); |
| 282 | |
| 283 | if (!validate_rtt_structure_cmds(map_addr, level, rd)) { |
| 284 | buffer_unmap(rd); |
| 285 | granule_unlock(g_rd); |
| 286 | return RMI_ERROR_INPUT; |
| 287 | } |
| 288 | |
| 289 | g_table_root = rd->s2_ctx.g_rtt; |
| 290 | sl = realm_rtt_starting_level(rd); |
| 291 | ipa_bits = realm_ipa_bits(rd); |
| 292 | s2_ctx = rd->s2_ctx; |
| 293 | buffer_unmap(rd); |
| 294 | granule_lock(g_table_root, GRANULE_STATE_RTT); |
| 295 | granule_unlock(g_rd); |
| 296 | |
| 297 | rtt_walk_lock_unlock(g_table_root, sl, ipa_bits, |
| 298 | map_addr, level - 1L, &wi); |
| 299 | if (wi.last_level != level - 1UL) { |
| 300 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 301 | goto out_unlock_parent_table; |
| 302 | } |
| 303 | |
| 304 | parent_s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 305 | parent_s2tte = s2tte_read(&parent_s2tt[wi.index]); |
| 306 | if (!s2tte_is_table(parent_s2tte, level - 1L)) { |
| 307 | ret = pack_return_code(RMI_ERROR_RTT, |
| 308 | (unsigned int)(level - 1L)); |
| 309 | goto out_unmap_parent_table; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Check that the 'rtt_addr' RTT is used at (map_addr, level). |
| 314 | * Note that this also verifies that the rtt_addr is properly aligned. |
| 315 | */ |
| 316 | if (rtt_addr != s2tte_pa_table(parent_s2tte, level - 1L)) { |
| 317 | ret = pack_return_code(RMI_ERROR_RTT, |
| 318 | (unsigned int)(level - 1L)); |
| 319 | goto out_unmap_parent_table; |
| 320 | } |
| 321 | |
| 322 | g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT); |
| 323 | |
| 324 | /* |
| 325 | * A table descriptor S2TTE always points to a TABLE granule. |
| 326 | */ |
| 327 | assert(g_tbl); |
| 328 | |
| 329 | table = granule_map(g_tbl, SLOT_RTT2); |
| 330 | |
| 331 | /* |
| 332 | * The command can succeed only if all 512 S2TTEs are of the same type. |
| 333 | * We first check the table's ref. counter to speed up the case when |
| 334 | * the host makes a guess whether a memory region can be folded. |
| 335 | */ |
| 336 | if (g_tbl->refcount == 0UL) { |
| 337 | if (table_is_destroyed_block(table)) { |
| 338 | parent_s2tte = s2tte_create_destroyed(); |
| 339 | __granule_put(wi.g_llt); |
| 340 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 341 | } else if (table_is_unassigned_empty_block(table)) { |
| 342 | parent_s2tte = s2tte_create_unassigned_empty(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 343 | __granule_put(wi.g_llt); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 344 | |
| 345 | } else if (table_is_unassigned_ram_block(table)) { |
| 346 | parent_s2tte = s2tte_create_unassigned_ram(); |
| 347 | __granule_put(wi.g_llt); |
| 348 | |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 349 | } else if (table_is_unassigned_ns_block(table)) { |
| 350 | parent_s2tte = s2tte_create_unassigned_ns(); |
| 351 | __granule_put(wi.g_llt); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 352 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 353 | } else { |
| 354 | /* |
| 355 | * The table holds a mixture of destroyed and |
| 356 | * unassigned entries. |
| 357 | */ |
AlexeiFedorov | 892abce | 2023-04-06 16:32:12 +0100 | [diff] [blame] | 358 | ret = pack_return_code(RMI_ERROR_RTT, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 359 | goto out_unmap_table; |
| 360 | } |
| 361 | |
| 362 | } else if (g_tbl->refcount == S2TTES_PER_S2TT) { |
| 363 | |
| 364 | unsigned long s2tte, block_pa; |
| 365 | |
| 366 | /* The RMM specification does not allow creating block |
| 367 | * entries less than RTT_MIN_BLOCK_LEVEL even though |
| 368 | * permitted by the Arm Architecture. |
| 369 | * Hence ensure that the table being folded is at a level |
| 370 | * higher than the RTT_MIN_BLOCK_LEVEL. |
| 371 | * |
| 372 | * A fully populated table cannot be destroyed if that |
| 373 | * would create a block mapping below RTT_MIN_BLOCK_LEVEL. |
| 374 | */ |
| 375 | if (level <= RTT_MIN_BLOCK_LEVEL) { |
AlexeiFedorov | 892abce | 2023-04-06 16:32:12 +0100 | [diff] [blame] | 376 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 377 | goto out_unmap_table; |
| 378 | } |
| 379 | |
| 380 | s2tte = s2tte_read(&table[0]); |
AlexeiFedorov | 80c2f04 | 2022-11-25 14:54:46 +0000 | [diff] [blame] | 381 | block_pa = s2tte_pa(s2tte, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 382 | |
| 383 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 384 | * The table must also refer to a contiguous block through the |
| 385 | * same type of s2tte, either Assigned, Valid or Assigned_NS. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 386 | */ |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 387 | if (table_maps_assigned_empty_block(table, level)) { |
| 388 | parent_s2tte = s2tte_create_assigned_empty(block_pa, |
| 389 | level - 1L); |
| 390 | } else if (table_maps_assigned_ram_block(table, level)) { |
| 391 | parent_s2tte = s2tte_create_assigned_ram(block_pa, |
| 392 | level - 1L); |
| 393 | } else if (table_maps_assigned_ns_block(table, level)) { |
| 394 | parent_s2tte = s2tte_create_assigned_ns(block_pa, |
| 395 | level - 1L); |
AlexeiFedorov | 80c2f04 | 2022-11-25 14:54:46 +0000 | [diff] [blame] | 396 | /* The table contains mixed entries that cannot be folded */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 397 | } else { |
AlexeiFedorov | 80c2f04 | 2022-11-25 14:54:46 +0000 | [diff] [blame] | 398 | ret = pack_return_code(RMI_ERROR_RTT, level); |
| 399 | goto out_unmap_table; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | __granule_refcount_dec(g_tbl, S2TTES_PER_S2TT); |
| 403 | } else { |
| 404 | /* |
| 405 | * The table holds a mixture of different types of s2ttes. |
| 406 | */ |
AlexeiFedorov | 892abce | 2023-04-06 16:32:12 +0100 | [diff] [blame] | 407 | ret = pack_return_code(RMI_ERROR_RTT, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 408 | goto out_unmap_table; |
| 409 | } |
| 410 | |
| 411 | ret = RMI_SUCCESS; |
| 412 | |
| 413 | /* |
| 414 | * Break before make. |
| 415 | */ |
| 416 | s2tte_write(&parent_s2tt[wi.index], 0UL); |
| 417 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 418 | if (s2tte_is_assigned_ram(parent_s2tte, level - 1L) || |
| 419 | s2tte_is_assigned_ns(parent_s2tte, level - 1L)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 420 | invalidate_pages_in_block(&s2_ctx, map_addr); |
| 421 | } else { |
| 422 | invalidate_block(&s2_ctx, map_addr); |
| 423 | } |
| 424 | |
| 425 | s2tte_write(&parent_s2tt[wi.index], parent_s2tte); |
| 426 | |
| 427 | granule_memzero_mapped(table); |
| 428 | granule_set_state(g_tbl, GRANULE_STATE_DELEGATED); |
| 429 | |
| 430 | out_unmap_table: |
| 431 | buffer_unmap(table); |
| 432 | granule_unlock(g_tbl); |
| 433 | out_unmap_parent_table: |
| 434 | buffer_unmap(parent_s2tt); |
| 435 | out_unlock_parent_table: |
| 436 | granule_unlock(wi.g_llt); |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | unsigned long smc_rtt_destroy(unsigned long rtt_addr, |
| 441 | unsigned long rd_addr, |
| 442 | unsigned long map_addr, |
| 443 | unsigned long ulevel) |
| 444 | { |
| 445 | struct granule *g_rd; |
| 446 | struct granule *g_tbl; |
| 447 | struct rd *rd; |
| 448 | struct granule *g_table_root; |
| 449 | struct rtt_walk wi; |
| 450 | unsigned long *table, *parent_s2tt, parent_s2tte; |
| 451 | long level = (long)ulevel; |
| 452 | unsigned long ipa_bits; |
| 453 | unsigned long ret; |
| 454 | struct realm_s2_context s2_ctx; |
| 455 | int sl; |
| 456 | bool in_par; |
| 457 | |
| 458 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 459 | if (g_rd == NULL) { |
| 460 | return RMI_ERROR_INPUT; |
| 461 | } |
| 462 | |
| 463 | rd = granule_map(g_rd, SLOT_RD); |
| 464 | |
| 465 | if (!validate_rtt_structure_cmds(map_addr, level, rd)) { |
| 466 | buffer_unmap(rd); |
| 467 | granule_unlock(g_rd); |
| 468 | return RMI_ERROR_INPUT; |
| 469 | } |
| 470 | |
| 471 | g_table_root = rd->s2_ctx.g_rtt; |
| 472 | sl = realm_rtt_starting_level(rd); |
| 473 | ipa_bits = realm_ipa_bits(rd); |
| 474 | s2_ctx = rd->s2_ctx; |
| 475 | in_par = addr_in_par(rd, map_addr); |
| 476 | buffer_unmap(rd); |
| 477 | granule_lock(g_table_root, GRANULE_STATE_RTT); |
| 478 | granule_unlock(g_rd); |
| 479 | |
| 480 | rtt_walk_lock_unlock(g_table_root, sl, ipa_bits, |
| 481 | map_addr, level - 1L, &wi); |
| 482 | if (wi.last_level != level - 1UL) { |
| 483 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 484 | goto out_unlock_parent_table; |
| 485 | } |
| 486 | |
| 487 | parent_s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 488 | parent_s2tte = s2tte_read(&parent_s2tt[wi.index]); |
| 489 | if (!s2tte_is_table(parent_s2tte, level - 1L)) { |
| 490 | ret = pack_return_code(RMI_ERROR_RTT, |
| 491 | (unsigned int)(level - 1L)); |
| 492 | goto out_unmap_parent_table; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Check that the 'rtt_addr' RTT is used at (map_addr, level). |
| 497 | * Note that this also verifies that the rtt_addr is properly aligned. |
| 498 | */ |
| 499 | if (rtt_addr != s2tte_pa_table(parent_s2tte, level - 1L)) { |
| 500 | ret = RMI_ERROR_INPUT; |
| 501 | goto out_unmap_parent_table; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Lock the RTT granule. The 'rtt_addr' is verified, thus can be treated |
| 506 | * as an internal granule. |
| 507 | */ |
| 508 | g_tbl = find_lock_granule(rtt_addr, GRANULE_STATE_RTT); |
| 509 | |
| 510 | /* |
| 511 | * A table descriptor S2TTE always points to a TABLE granule. |
| 512 | */ |
| 513 | assert(g_tbl != NULL); |
| 514 | |
| 515 | /* |
| 516 | * Read the refcount value. RTT granule is always accessed locked, thus |
| 517 | * the refcount can be accessed without atomic operations. |
| 518 | */ |
| 519 | if (g_tbl->refcount != 0UL) { |
AlexeiFedorov | 892abce | 2023-04-06 16:32:12 +0100 | [diff] [blame] | 520 | ret = pack_return_code(RMI_ERROR_RTT, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 521 | goto out_unlock_table; |
| 522 | } |
| 523 | |
| 524 | ret = RMI_SUCCESS; |
| 525 | |
| 526 | table = granule_map(g_tbl, SLOT_RTT2); |
| 527 | |
| 528 | if (in_par) { |
| 529 | parent_s2tte = s2tte_create_destroyed(); |
| 530 | } else { |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 531 | parent_s2tte = s2tte_create_unassigned_ns(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | __granule_put(wi.g_llt); |
| 535 | |
| 536 | /* |
| 537 | * Break before make. Note that this may cause spurious S2 aborts. |
| 538 | */ |
| 539 | s2tte_write(&parent_s2tt[wi.index], 0UL); |
| 540 | invalidate_block(&s2_ctx, map_addr); |
| 541 | s2tte_write(&parent_s2tt[wi.index], parent_s2tte); |
| 542 | |
| 543 | granule_memzero_mapped(table); |
| 544 | granule_set_state(g_tbl, GRANULE_STATE_DELEGATED); |
| 545 | |
| 546 | buffer_unmap(table); |
| 547 | out_unlock_table: |
| 548 | granule_unlock(g_tbl); |
| 549 | out_unmap_parent_table: |
| 550 | buffer_unmap(parent_s2tt); |
| 551 | out_unlock_parent_table: |
| 552 | granule_unlock(wi.g_llt); |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | enum map_unmap_ns_op { |
| 557 | MAP_NS, |
| 558 | UNMAP_NS |
| 559 | }; |
| 560 | |
| 561 | /* |
| 562 | * We don't hold a reference on the NS granule when it is |
| 563 | * mapped into a realm. Instead we rely on the guarantees |
| 564 | * provided by the architecture to ensure that a NS access |
| 565 | * to a protected granule is prohibited even within the realm. |
| 566 | */ |
| 567 | static unsigned long map_unmap_ns(unsigned long rd_addr, |
| 568 | unsigned long map_addr, |
| 569 | long level, |
| 570 | unsigned long host_s2tte, |
| 571 | enum map_unmap_ns_op op) |
| 572 | { |
| 573 | struct granule *g_rd; |
| 574 | struct rd *rd; |
| 575 | struct granule *g_table_root; |
| 576 | unsigned long *s2tt, s2tte; |
| 577 | struct rtt_walk wi; |
| 578 | unsigned long ipa_bits; |
| 579 | unsigned long ret; |
| 580 | struct realm_s2_context s2_ctx; |
| 581 | int sl; |
| 582 | |
| 583 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 584 | if (g_rd == NULL) { |
| 585 | return RMI_ERROR_INPUT; |
| 586 | } |
| 587 | |
| 588 | rd = granule_map(g_rd, SLOT_RD); |
| 589 | |
| 590 | if (!validate_rtt_map_cmds(map_addr, level, rd)) { |
| 591 | buffer_unmap(rd); |
| 592 | granule_unlock(g_rd); |
| 593 | return RMI_ERROR_INPUT; |
| 594 | } |
| 595 | |
| 596 | g_table_root = rd->s2_ctx.g_rtt; |
| 597 | sl = realm_rtt_starting_level(rd); |
| 598 | ipa_bits = realm_ipa_bits(rd); |
| 599 | |
AlexeiFedorov | c34e324 | 2023-04-12 11:30:33 +0100 | [diff] [blame] | 600 | /* Check if map_addr is outside PAR */ |
| 601 | if (addr_in_par(rd, map_addr)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 602 | buffer_unmap(rd); |
| 603 | granule_unlock(g_rd); |
| 604 | return RMI_ERROR_INPUT; |
| 605 | } |
| 606 | |
| 607 | s2_ctx = rd->s2_ctx; |
| 608 | buffer_unmap(rd); |
| 609 | |
| 610 | granule_lock(g_table_root, GRANULE_STATE_RTT); |
| 611 | granule_unlock(g_rd); |
| 612 | |
| 613 | rtt_walk_lock_unlock(g_table_root, sl, ipa_bits, |
| 614 | map_addr, level, &wi); |
| 615 | if (wi.last_level != level) { |
| 616 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 617 | goto out_unlock_llt; |
| 618 | } |
| 619 | |
| 620 | s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 621 | s2tte = s2tte_read(&s2tt[wi.index]); |
| 622 | |
| 623 | if (op == MAP_NS) { |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 624 | if (!s2tte_is_unassigned_ns(s2tte)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 625 | ret = pack_return_code(RMI_ERROR_RTT, |
| 626 | (unsigned int)level); |
| 627 | goto out_unmap_table; |
| 628 | } |
| 629 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 630 | s2tte = s2tte_create_assigned_ns(host_s2tte, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 631 | s2tte_write(&s2tt[wi.index], s2tte); |
| 632 | __granule_get(wi.g_llt); |
| 633 | |
| 634 | } else if (op == UNMAP_NS) { |
| 635 | /* |
| 636 | * The following check also verifies that map_addr is outside |
| 637 | * PAR, as valid_NS s2tte may only cover outside PAR IPA range. |
| 638 | */ |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 639 | if (!s2tte_is_assigned_ns(s2tte, level)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 640 | ret = pack_return_code(RMI_ERROR_RTT, |
| 641 | (unsigned int)level); |
| 642 | goto out_unmap_table; |
| 643 | } |
| 644 | |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 645 | s2tte = s2tte_create_unassigned_ns(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 646 | s2tte_write(&s2tt[wi.index], s2tte); |
| 647 | __granule_put(wi.g_llt); |
| 648 | if (level == RTT_PAGE_LEVEL) { |
| 649 | invalidate_page(&s2_ctx, map_addr); |
| 650 | } else { |
| 651 | invalidate_block(&s2_ctx, map_addr); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | ret = RMI_SUCCESS; |
| 656 | |
| 657 | out_unmap_table: |
| 658 | buffer_unmap(s2tt); |
| 659 | out_unlock_llt: |
| 660 | granule_unlock(wi.g_llt); |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | unsigned long smc_rtt_map_unprotected(unsigned long rd_addr, |
| 665 | unsigned long map_addr, |
| 666 | unsigned long ulevel, |
| 667 | unsigned long s2tte) |
| 668 | { |
| 669 | long level = (long)ulevel; |
| 670 | |
| 671 | if (!host_ns_s2tte_is_valid(s2tte, level)) { |
| 672 | return RMI_ERROR_INPUT; |
| 673 | } |
| 674 | |
| 675 | return map_unmap_ns(rd_addr, map_addr, level, s2tte, MAP_NS); |
| 676 | } |
| 677 | |
| 678 | unsigned long smc_rtt_unmap_unprotected(unsigned long rd_addr, |
| 679 | unsigned long map_addr, |
| 680 | unsigned long ulevel) |
| 681 | { |
| 682 | return map_unmap_ns(rd_addr, map_addr, (long)ulevel, 0UL, UNMAP_NS); |
| 683 | } |
| 684 | |
| 685 | void smc_rtt_read_entry(unsigned long rd_addr, |
| 686 | unsigned long map_addr, |
| 687 | unsigned long ulevel, |
| 688 | struct smc_result *ret) |
| 689 | { |
| 690 | struct granule *g_rd, *g_rtt_root; |
| 691 | struct rd *rd; |
| 692 | struct rtt_walk wi; |
| 693 | unsigned long *s2tt, s2tte; |
| 694 | unsigned long ipa_bits; |
| 695 | long level = (long)ulevel; |
| 696 | int sl; |
| 697 | |
| 698 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 699 | if (g_rd == NULL) { |
| 700 | ret->x[0] = RMI_ERROR_INPUT; |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | rd = granule_map(g_rd, SLOT_RD); |
| 705 | |
| 706 | if (!validate_rtt_entry_cmds(map_addr, level, rd)) { |
| 707 | buffer_unmap(rd); |
| 708 | granule_unlock(g_rd); |
| 709 | ret->x[0] = RMI_ERROR_INPUT; |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | g_rtt_root = rd->s2_ctx.g_rtt; |
| 714 | sl = realm_rtt_starting_level(rd); |
| 715 | ipa_bits = realm_ipa_bits(rd); |
| 716 | buffer_unmap(rd); |
| 717 | |
| 718 | granule_lock(g_rtt_root, GRANULE_STATE_RTT); |
| 719 | granule_unlock(g_rd); |
| 720 | |
| 721 | rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits, |
| 722 | map_addr, level, &wi); |
| 723 | s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 724 | s2tte = s2tte_read(&s2tt[wi.index]); |
| 725 | ret->x[1] = wi.last_level; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 726 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 727 | if (s2tte_is_unassigned_empty(s2tte)) { |
Yousuf A | 3daed82 | 2022-10-13 16:06:00 +0100 | [diff] [blame] | 728 | ret->x[2] = RMI_UNASSIGNED; |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 729 | ret->x[3] = 0UL; |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 730 | ret->x[4] = RIPAS_EMPTY; |
| 731 | } else if (s2tte_is_unassigned_ram(s2tte)) { |
| 732 | ret->x[2] = RMI_UNASSIGNED; |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 733 | ret->x[3] = 0UL; |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 734 | ret->x[4] = RIPAS_RAM; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 735 | } else if (s2tte_is_destroyed(s2tte)) { |
Yousuf A | 3daed82 | 2022-10-13 16:06:00 +0100 | [diff] [blame] | 736 | ret->x[2] = RMI_DESTROYED; |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 737 | ret->x[3] = 0UL; |
| 738 | ret->x[4] = RIPAS_UNDEFINED; |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 739 | } else if (s2tte_is_assigned_empty(s2tte, wi.last_level)) { |
Yousuf A | 3daed82 | 2022-10-13 16:06:00 +0100 | [diff] [blame] | 740 | ret->x[2] = RMI_ASSIGNED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 741 | ret->x[3] = s2tte_pa(s2tte, wi.last_level); |
Yousuf A | 6280815 | 2022-10-31 10:35:42 +0000 | [diff] [blame] | 742 | ret->x[4] = RIPAS_EMPTY; |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 743 | } else if (s2tte_is_assigned_ram(s2tte, wi.last_level)) { |
Yousuf A | 3daed82 | 2022-10-13 16:06:00 +0100 | [diff] [blame] | 744 | ret->x[2] = RMI_ASSIGNED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 745 | ret->x[3] = s2tte_pa(s2tte, wi.last_level); |
Yousuf A | 6280815 | 2022-10-31 10:35:42 +0000 | [diff] [blame] | 746 | ret->x[4] = RIPAS_RAM; |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 747 | } else if (s2tte_is_unassigned_ns(s2tte)) { |
| 748 | ret->x[2] = RMI_UNASSIGNED; |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 749 | ret->x[3] = 0UL; |
| 750 | ret->x[4] = RIPAS_UNDEFINED; |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 751 | } else if (s2tte_is_assigned_ns(s2tte, wi.last_level)) { |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 752 | ret->x[2] = RMI_ASSIGNED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 753 | ret->x[3] = host_ns_s2tte(s2tte, wi.last_level); |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 754 | ret->x[4] = RIPAS_UNDEFINED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 755 | } else if (s2tte_is_table(s2tte, wi.last_level)) { |
Yousuf A | 3daed82 | 2022-10-13 16:06:00 +0100 | [diff] [blame] | 756 | ret->x[2] = RMI_TABLE; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 757 | ret->x[3] = s2tte_pa_table(s2tte, wi.last_level); |
AlexeiFedorov | 20afb5c | 2023-04-18 11:44:19 +0100 | [diff] [blame^] | 758 | ret->x[4] = RIPAS_UNDEFINED; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 759 | } else { |
| 760 | assert(false); |
| 761 | } |
| 762 | |
| 763 | buffer_unmap(s2tt); |
| 764 | granule_unlock(wi.g_llt); |
| 765 | |
| 766 | ret->x[0] = RMI_SUCCESS; |
| 767 | } |
| 768 | |
| 769 | static void data_granule_measure(struct rd *rd, void *data, |
| 770 | unsigned long ipa, |
| 771 | unsigned long flags) |
| 772 | { |
| 773 | struct measurement_desc_data measure_desc = {0}; |
| 774 | |
| 775 | /* Initialize the measurement descriptior structure */ |
| 776 | measure_desc.desc_type = MEASURE_DESC_TYPE_DATA; |
| 777 | measure_desc.len = sizeof(struct measurement_desc_data); |
| 778 | measure_desc.ipa = ipa; |
| 779 | measure_desc.flags = flags; |
| 780 | memcpy(measure_desc.rim, |
| 781 | &rd->measurement[RIM_MEASUREMENT_SLOT], |
| 782 | measurement_get_size(rd->algorithm)); |
| 783 | |
| 784 | if (flags == RMI_MEASURE_CONTENT) { |
| 785 | /* |
| 786 | * Hashing the data granules and store the result in the |
| 787 | * measurement descriptor structure. |
| 788 | */ |
| 789 | measurement_hash_compute(rd->algorithm, |
| 790 | data, |
| 791 | GRANULE_SIZE, |
| 792 | measure_desc.content); |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Hashing the measurement descriptor structure; the result is the |
| 797 | * updated RIM. |
| 798 | */ |
| 799 | measurement_hash_compute(rd->algorithm, |
| 800 | &measure_desc, |
| 801 | sizeof(measure_desc), |
| 802 | rd->measurement[RIM_MEASUREMENT_SLOT]); |
| 803 | } |
| 804 | |
| 805 | static unsigned long validate_data_create_unknown(unsigned long map_addr, |
| 806 | struct rd *rd) |
| 807 | { |
| 808 | if (!addr_in_par(rd, map_addr)) { |
| 809 | return RMI_ERROR_INPUT; |
| 810 | } |
| 811 | |
| 812 | if (!validate_map_addr(map_addr, RTT_PAGE_LEVEL, rd)) { |
| 813 | return RMI_ERROR_INPUT; |
| 814 | } |
| 815 | |
| 816 | return RMI_SUCCESS; |
| 817 | } |
| 818 | |
| 819 | static unsigned long validate_data_create(unsigned long map_addr, |
| 820 | struct rd *rd) |
| 821 | { |
| 822 | if (get_rd_state_locked(rd) != REALM_STATE_NEW) { |
| 823 | return RMI_ERROR_REALM; |
| 824 | } |
| 825 | |
| 826 | return validate_data_create_unknown(map_addr, rd); |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Implements both Data.Create and Data.CreateUnknown |
| 831 | * |
| 832 | * if @g_src == NULL, this implemented Data.CreateUnknown |
| 833 | * and otherwise this implemented Data.Create. |
| 834 | */ |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 835 | static unsigned long data_create(unsigned long rd_addr, |
| 836 | unsigned long data_addr, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 837 | unsigned long map_addr, |
| 838 | struct granule *g_src, |
| 839 | unsigned long flags) |
| 840 | { |
| 841 | struct granule *g_data; |
| 842 | struct granule *g_rd; |
| 843 | struct granule *g_table_root; |
| 844 | struct rd *rd; |
| 845 | struct rtt_walk wi; |
| 846 | unsigned long s2tte, *s2tt; |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 847 | enum ripas ripas_val; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 848 | enum granule_state new_data_state = GRANULE_STATE_DELEGATED; |
| 849 | unsigned long ipa_bits; |
| 850 | unsigned long ret; |
| 851 | int __unused meas_ret; |
| 852 | int sl; |
| 853 | |
| 854 | if (!find_lock_two_granules(data_addr, |
| 855 | GRANULE_STATE_DELEGATED, |
| 856 | &g_data, |
| 857 | rd_addr, |
| 858 | GRANULE_STATE_RD, |
| 859 | &g_rd)) { |
| 860 | return RMI_ERROR_INPUT; |
| 861 | } |
| 862 | |
| 863 | rd = granule_map(g_rd, SLOT_RD); |
| 864 | |
| 865 | ret = (g_src != NULL) ? |
| 866 | validate_data_create(map_addr, rd) : |
| 867 | validate_data_create_unknown(map_addr, rd); |
| 868 | |
| 869 | if (ret != RMI_SUCCESS) { |
| 870 | goto out_unmap_rd; |
| 871 | } |
| 872 | |
| 873 | g_table_root = rd->s2_ctx.g_rtt; |
| 874 | sl = realm_rtt_starting_level(rd); |
| 875 | ipa_bits = realm_ipa_bits(rd); |
| 876 | granule_lock(g_table_root, GRANULE_STATE_RTT); |
| 877 | rtt_walk_lock_unlock(g_table_root, sl, ipa_bits, |
| 878 | map_addr, RTT_PAGE_LEVEL, &wi); |
| 879 | if (wi.last_level != RTT_PAGE_LEVEL) { |
| 880 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 881 | goto out_unlock_ll_table; |
| 882 | } |
| 883 | |
| 884 | s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 885 | s2tte = s2tte_read(&s2tt[wi.index]); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 886 | if (s2tte_is_unassigned_empty(s2tte)) { |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 887 | ripas_val = RIPAS_EMPTY; |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 888 | } else if (s2tte_is_unassigned_ram(s2tte)) { |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 889 | ripas_val = RIPAS_RAM; |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 890 | } else { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 891 | ret = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL); |
| 892 | goto out_unmap_ll_table; |
| 893 | } |
| 894 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 895 | ripas_val = s2tte_get_ripas(s2tte); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 896 | |
| 897 | if (g_src != NULL) { |
| 898 | bool ns_access_ok; |
| 899 | void *data = granule_map(g_data, SLOT_DELEGATED); |
| 900 | |
| 901 | ns_access_ok = ns_buffer_read(SLOT_NS, g_src, 0U, |
| 902 | GRANULE_SIZE, data); |
| 903 | |
| 904 | if (!ns_access_ok) { |
| 905 | /* |
| 906 | * Some data may be copied before the failure. Zero |
| 907 | * g_data granule as it will remain in delegated state. |
| 908 | */ |
| 909 | (void)memset(data, 0, GRANULE_SIZE); |
| 910 | buffer_unmap(data); |
| 911 | ret = RMI_ERROR_INPUT; |
| 912 | goto out_unmap_ll_table; |
| 913 | } |
| 914 | |
| 915 | |
| 916 | data_granule_measure(rd, data, map_addr, flags); |
| 917 | |
| 918 | buffer_unmap(data); |
| 919 | } |
| 920 | |
| 921 | new_data_state = GRANULE_STATE_DATA; |
| 922 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 923 | s2tte = (ripas_val == RIPAS_EMPTY) ? |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 924 | s2tte_create_assigned_empty(data_addr, RTT_PAGE_LEVEL) : |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 925 | s2tte_create_assigned_ram(data_addr, RTT_PAGE_LEVEL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 926 | |
| 927 | s2tte_write(&s2tt[wi.index], s2tte); |
| 928 | __granule_get(wi.g_llt); |
| 929 | |
| 930 | ret = RMI_SUCCESS; |
| 931 | |
| 932 | out_unmap_ll_table: |
| 933 | buffer_unmap(s2tt); |
| 934 | out_unlock_ll_table: |
| 935 | granule_unlock(wi.g_llt); |
| 936 | out_unmap_rd: |
| 937 | buffer_unmap(rd); |
| 938 | granule_unlock(g_rd); |
| 939 | granule_unlock_transition(g_data, new_data_state); |
| 940 | return ret; |
| 941 | } |
| 942 | |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 943 | unsigned long smc_data_create(unsigned long rd_addr, |
| 944 | unsigned long data_addr, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 945 | unsigned long map_addr, |
| 946 | unsigned long src_addr, |
| 947 | unsigned long flags) |
| 948 | { |
| 949 | struct granule *g_src; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 950 | |
| 951 | if (flags != RMI_NO_MEASURE_CONTENT && flags != RMI_MEASURE_CONTENT) { |
| 952 | return RMI_ERROR_INPUT; |
| 953 | } |
| 954 | |
| 955 | g_src = find_granule(src_addr); |
| 956 | if ((g_src == NULL) || (g_src->state != GRANULE_STATE_NS)) { |
| 957 | return RMI_ERROR_INPUT; |
| 958 | } |
| 959 | |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 960 | return data_create(rd_addr, data_addr, map_addr, g_src, flags); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 961 | } |
| 962 | |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 963 | unsigned long smc_data_create_unknown(unsigned long rd_addr, |
| 964 | unsigned long data_addr, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 965 | unsigned long map_addr) |
| 966 | { |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 967 | return data_create(rd_addr, data_addr, map_addr, NULL, 0); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | unsigned long smc_data_destroy(unsigned long rd_addr, |
| 971 | unsigned long map_addr) |
| 972 | { |
| 973 | struct granule *g_data; |
| 974 | struct granule *g_rd; |
| 975 | struct granule *g_table_root; |
| 976 | struct rtt_walk wi; |
| 977 | unsigned long data_addr, s2tte, *s2tt; |
| 978 | struct rd *rd; |
| 979 | unsigned long ipa_bits; |
| 980 | unsigned long ret; |
| 981 | struct realm_s2_context s2_ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 982 | int sl; |
| 983 | |
| 984 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 985 | if (g_rd == NULL) { |
| 986 | return RMI_ERROR_INPUT; |
| 987 | } |
| 988 | |
| 989 | rd = granule_map(g_rd, SLOT_RD); |
| 990 | |
| 991 | if (!validate_map_addr(map_addr, RTT_PAGE_LEVEL, rd)) { |
| 992 | buffer_unmap(rd); |
| 993 | granule_unlock(g_rd); |
| 994 | return RMI_ERROR_INPUT; |
| 995 | } |
| 996 | |
| 997 | g_table_root = rd->s2_ctx.g_rtt; |
| 998 | sl = realm_rtt_starting_level(rd); |
| 999 | ipa_bits = realm_ipa_bits(rd); |
| 1000 | s2_ctx = rd->s2_ctx; |
| 1001 | buffer_unmap(rd); |
| 1002 | |
| 1003 | granule_lock(g_table_root, GRANULE_STATE_RTT); |
| 1004 | granule_unlock(g_rd); |
| 1005 | |
| 1006 | rtt_walk_lock_unlock(g_table_root, sl, ipa_bits, |
| 1007 | map_addr, RTT_PAGE_LEVEL, &wi); |
| 1008 | if (wi.last_level != RTT_PAGE_LEVEL) { |
| 1009 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 1010 | goto out_unlock_ll_table; |
| 1011 | } |
| 1012 | |
| 1013 | s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 1014 | s2tte = s2tte_read(&s2tt[wi.index]); |
| 1015 | |
AlexeiFedorov | a43cd31 | 2023-04-17 11:42:25 +0100 | [diff] [blame] | 1016 | if (s2tte_is_assigned_ram(s2tte, RTT_PAGE_LEVEL)) { |
| 1017 | data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL); |
| 1018 | s2tte = s2tte_create_destroyed(); |
| 1019 | s2tte_write(&s2tt[wi.index], s2tte); |
| 1020 | invalidate_page(&s2_ctx, map_addr); |
| 1021 | } else if (s2tte_is_assigned_empty(s2tte, RTT_PAGE_LEVEL)) { |
| 1022 | data_addr = s2tte_pa(s2tte, RTT_PAGE_LEVEL); |
| 1023 | s2tte = s2tte_create_unassigned_empty(); |
| 1024 | s2tte_write(&s2tt[wi.index], s2tte); |
| 1025 | } else { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1026 | ret = pack_return_code(RMI_ERROR_RTT, RTT_PAGE_LEVEL); |
| 1027 | goto out_unmap_ll_table; |
| 1028 | } |
| 1029 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1030 | __granule_put(wi.g_llt); |
| 1031 | |
| 1032 | /* |
| 1033 | * Lock the data granule and check expected state. Correct locking order |
| 1034 | * is guaranteed because granule address is obtained from a locked |
| 1035 | * granule by table walk. This lock needs to be acquired before a state |
| 1036 | * transition to or from GRANULE_STATE_DATA for granule address can happen. |
| 1037 | */ |
| 1038 | g_data = find_lock_granule(data_addr, GRANULE_STATE_DATA); |
| 1039 | assert(g_data); |
| 1040 | granule_memzero(g_data, SLOT_DELEGATED); |
| 1041 | granule_unlock_transition(g_data, GRANULE_STATE_DELEGATED); |
| 1042 | |
| 1043 | ret = RMI_SUCCESS; |
| 1044 | |
| 1045 | out_unmap_ll_table: |
| 1046 | buffer_unmap(s2tt); |
| 1047 | out_unlock_ll_table: |
| 1048 | granule_unlock(wi.g_llt); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1053 | /* |
| 1054 | * Sets new ripas value in @s2tte. |
| 1055 | * |
| 1056 | * It returns false if the @s2tte has no ripas value. |
| 1057 | * It sets @(*do_tlbi) to 'true' if the TLBs have to be invalidated. |
| 1058 | */ |
| 1059 | static bool update_ripas(unsigned long *s2tte, bool *do_tlbi, |
| 1060 | unsigned long level, enum ripas ripas_val) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1061 | { |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1062 | unsigned long pa; |
| 1063 | |
| 1064 | *do_tlbi = false; |
| 1065 | |
| 1066 | if (!s2tte_has_ripas(*s2tte, level)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1067 | return false; |
| 1068 | } |
| 1069 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1070 | if (ripas_val == RIPAS_RAM) { |
| 1071 | if (s2tte_is_unassigned_empty(*s2tte)) { |
| 1072 | *s2tte = s2tte_create_unassigned_ram(); |
| 1073 | } else if (s2tte_is_assigned_empty(*s2tte, level)) { |
| 1074 | pa = s2tte_pa(*s2tte, level); |
| 1075 | *s2tte = s2tte_create_assigned_ram(pa, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1076 | } |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1077 | } else if (ripas_val == RIPAS_EMPTY) { |
| 1078 | if (s2tte_is_unassigned_ram(*s2tte)) { |
| 1079 | *s2tte = s2tte_create_unassigned_empty(); |
| 1080 | } else if (s2tte_is_assigned_ram(*s2tte, level)) { |
| 1081 | pa = s2tte_pa(*s2tte, level); |
| 1082 | *s2tte = s2tte_create_assigned_empty(pa, level); |
| 1083 | *do_tlbi = true; |
| 1084 | } |
| 1085 | } else { |
| 1086 | assert(false); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1089 | return true; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | static void ripas_granule_measure(struct rd *rd, |
| 1093 | unsigned long ipa, |
| 1094 | unsigned long level) |
| 1095 | { |
| 1096 | struct measurement_desc_ripas measure_desc = {0}; |
| 1097 | |
| 1098 | /* Initialize the measurement descriptior structure */ |
| 1099 | measure_desc.desc_type = MEASURE_DESC_TYPE_RIPAS; |
| 1100 | measure_desc.len = sizeof(struct measurement_desc_ripas); |
| 1101 | measure_desc.ipa = ipa; |
| 1102 | measure_desc.level = level; |
| 1103 | memcpy(measure_desc.rim, |
| 1104 | &rd->measurement[RIM_MEASUREMENT_SLOT], |
| 1105 | measurement_get_size(rd->algorithm)); |
| 1106 | |
| 1107 | /* |
| 1108 | * Hashing the measurement descriptor structure; the result is the |
| 1109 | * updated RIM. |
| 1110 | */ |
| 1111 | measurement_hash_compute(rd->algorithm, |
| 1112 | &measure_desc, |
| 1113 | sizeof(measure_desc), |
| 1114 | rd->measurement[RIM_MEASUREMENT_SLOT]); |
| 1115 | } |
| 1116 | |
| 1117 | unsigned long smc_rtt_init_ripas(unsigned long rd_addr, |
| 1118 | unsigned long map_addr, |
| 1119 | unsigned long ulevel) |
| 1120 | { |
| 1121 | struct granule *g_rd, *g_rtt_root; |
| 1122 | struct rd *rd; |
| 1123 | unsigned long ipa_bits; |
| 1124 | struct rtt_walk wi; |
| 1125 | unsigned long s2tte, *s2tt; |
| 1126 | unsigned long ret; |
| 1127 | long level = (long)ulevel; |
| 1128 | int sl; |
| 1129 | |
| 1130 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 1131 | if (g_rd == NULL) { |
| 1132 | return RMI_ERROR_INPUT; |
| 1133 | } |
| 1134 | |
| 1135 | rd = granule_map(g_rd, SLOT_RD); |
| 1136 | |
| 1137 | if (get_rd_state_locked(rd) != REALM_STATE_NEW) { |
| 1138 | buffer_unmap(rd); |
| 1139 | granule_unlock(g_rd); |
| 1140 | return RMI_ERROR_REALM; |
| 1141 | } |
| 1142 | |
| 1143 | if (!validate_rtt_entry_cmds(map_addr, level, rd)) { |
| 1144 | buffer_unmap(rd); |
| 1145 | granule_unlock(g_rd); |
| 1146 | return RMI_ERROR_INPUT; |
| 1147 | } |
| 1148 | |
| 1149 | if (!addr_in_par(rd, map_addr)) { |
| 1150 | buffer_unmap(rd); |
| 1151 | granule_unlock(g_rd); |
| 1152 | return RMI_ERROR_INPUT; |
| 1153 | } |
| 1154 | |
| 1155 | g_rtt_root = rd->s2_ctx.g_rtt; |
| 1156 | sl = realm_rtt_starting_level(rd); |
| 1157 | ipa_bits = realm_ipa_bits(rd); |
| 1158 | |
| 1159 | granule_lock(g_rtt_root, GRANULE_STATE_RTT); |
| 1160 | granule_unlock(g_rd); |
| 1161 | |
| 1162 | rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits, |
| 1163 | map_addr, level, &wi); |
AlexeiFedorov | ac923c8 | 2023-04-06 15:12:04 +0100 | [diff] [blame] | 1164 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1165 | if (wi.last_level != level) { |
| 1166 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 1167 | goto out_unlock_llt; |
| 1168 | } |
| 1169 | |
| 1170 | s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 1171 | s2tte = s2tte_read(&s2tt[wi.index]); |
| 1172 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1173 | if (s2tte_is_unassigned_empty(s2tte)) { |
| 1174 | s2tte = s2tte_create_unassigned_ram(); |
| 1175 | s2tte_write(&s2tt[wi.index], s2tte); |
| 1176 | ripas_granule_measure(rd, map_addr, level); |
| 1177 | } else if (s2tte_is_unassigned_ram(s2tte)) { |
| 1178 | ripas_granule_measure(rd, map_addr, level); |
| 1179 | } else { |
| 1180 | ret = pack_return_code(RMI_ERROR_RTT, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1181 | goto out_unmap_llt; |
| 1182 | } |
| 1183 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1184 | ret = RMI_SUCCESS; |
| 1185 | |
| 1186 | out_unmap_llt: |
| 1187 | buffer_unmap(s2tt); |
| 1188 | out_unlock_llt: |
| 1189 | buffer_unmap(rd); |
| 1190 | granule_unlock(wi.g_llt); |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | unsigned long smc_rtt_set_ripas(unsigned long rd_addr, |
| 1195 | unsigned long rec_addr, |
| 1196 | unsigned long map_addr, |
| 1197 | unsigned long ulevel, |
| 1198 | unsigned long uripas) |
| 1199 | { |
| 1200 | struct granule *g_rd, *g_rec, *g_rtt_root; |
| 1201 | struct rec *rec; |
| 1202 | struct rd *rd; |
| 1203 | unsigned long map_size, ipa_bits; |
| 1204 | struct rtt_walk wi; |
| 1205 | unsigned long s2tte, *s2tt; |
| 1206 | struct realm_s2_context s2_ctx; |
| 1207 | long level = (long)ulevel; |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1208 | enum ripas ripas_val = (enum ripas)uripas; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1209 | unsigned long ret; |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1210 | bool tlbi_required; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1211 | int sl; |
| 1212 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1213 | if (ripas_val > RIPAS_RAM) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1214 | return RMI_ERROR_INPUT; |
| 1215 | } |
| 1216 | |
| 1217 | if (!find_lock_two_granules(rd_addr, |
| 1218 | GRANULE_STATE_RD, |
| 1219 | &g_rd, |
| 1220 | rec_addr, |
| 1221 | GRANULE_STATE_REC, |
| 1222 | &g_rec)) { |
| 1223 | return RMI_ERROR_INPUT; |
| 1224 | } |
| 1225 | |
| 1226 | if (granule_refcount_read_acquire(g_rec) != 0UL) { |
AlexeiFedorov | 892abce | 2023-04-06 16:32:12 +0100 | [diff] [blame] | 1227 | ret = RMI_ERROR_REC; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1228 | goto out_unlock_rec_rd; |
| 1229 | } |
| 1230 | |
| 1231 | rec = granule_map(g_rec, SLOT_REC); |
| 1232 | |
| 1233 | if (g_rd != rec->realm_info.g_rd) { |
| 1234 | ret = RMI_ERROR_REC; |
| 1235 | goto out_unmap_rec; |
| 1236 | } |
| 1237 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1238 | if (ripas_val != rec->set_ripas.ripas_val) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1239 | ret = RMI_ERROR_INPUT; |
| 1240 | goto out_unmap_rec; |
| 1241 | } |
| 1242 | |
| 1243 | if (map_addr != rec->set_ripas.addr) { |
| 1244 | /* Target region is not next chunk of requested region */ |
| 1245 | ret = RMI_ERROR_INPUT; |
| 1246 | goto out_unmap_rec; |
| 1247 | } |
| 1248 | |
| 1249 | rd = granule_map(g_rd, SLOT_RD); |
| 1250 | |
| 1251 | if (!validate_rtt_entry_cmds(map_addr, level, rd)) { |
| 1252 | ret = RMI_ERROR_INPUT; |
| 1253 | goto out_unmap_rd; |
| 1254 | } |
| 1255 | |
| 1256 | map_size = s2tte_map_size(level); |
| 1257 | if (map_addr + map_size > rec->set_ripas.end) { |
| 1258 | /* Target region extends beyond end of requested region */ |
| 1259 | ret = RMI_ERROR_INPUT; |
| 1260 | goto out_unmap_rd; |
| 1261 | } |
| 1262 | |
| 1263 | g_rtt_root = rd->s2_ctx.g_rtt; |
| 1264 | sl = realm_rtt_starting_level(rd); |
| 1265 | ipa_bits = realm_ipa_bits(rd); |
| 1266 | s2_ctx = rd->s2_ctx; |
| 1267 | |
| 1268 | granule_lock(g_rtt_root, GRANULE_STATE_RTT); |
| 1269 | |
| 1270 | rtt_walk_lock_unlock(g_rtt_root, sl, ipa_bits, |
| 1271 | map_addr, level, &wi); |
| 1272 | if (wi.last_level != level) { |
| 1273 | ret = pack_return_code(RMI_ERROR_RTT, wi.last_level); |
| 1274 | goto out_unlock_llt; |
| 1275 | } |
| 1276 | |
| 1277 | s2tt = granule_map(wi.g_llt, SLOT_RTT); |
| 1278 | s2tte = s2tte_read(&s2tt[wi.index]); |
| 1279 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1280 | if (!update_ripas(&s2tte, &tlbi_required, level, ripas_val)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1281 | ret = pack_return_code(RMI_ERROR_RTT, (unsigned int)level); |
| 1282 | goto out_unmap_llt; |
| 1283 | } |
| 1284 | |
| 1285 | s2tte_write(&s2tt[wi.index], s2tte); |
| 1286 | |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 1287 | if (tlbi_required) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1288 | if (level == RTT_PAGE_LEVEL) { |
| 1289 | invalidate_page(&s2_ctx, map_addr); |
| 1290 | } else { |
| 1291 | invalidate_block(&s2_ctx, map_addr); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | rec->set_ripas.addr += map_size; |
| 1296 | |
| 1297 | ret = RMI_SUCCESS; |
| 1298 | |
| 1299 | out_unmap_llt: |
| 1300 | buffer_unmap(s2tt); |
| 1301 | out_unlock_llt: |
| 1302 | granule_unlock(wi.g_llt); |
| 1303 | out_unmap_rd: |
| 1304 | buffer_unmap(rd); |
| 1305 | out_unmap_rec: |
| 1306 | buffer_unmap(rec); |
| 1307 | out_unlock_rec_rd: |
| 1308 | granule_unlock(g_rec); |
| 1309 | granule_unlock(g_rd); |
| 1310 | return ret; |
| 1311 | } |