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