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