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