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