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