Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * |
| 4 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 8 | #include <arch_features.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 9 | #include <arch_helpers.h> |
| 10 | #include <debug.h> |
| 11 | #include <esr.h> |
| 12 | #include <memory_alloc.h> |
| 13 | #include <rec.h> |
| 14 | #include <smc-rmi.h> |
| 15 | |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 16 | #define SYSREG_CASE(reg) \ |
| 17 | case ESR_EL2_SYSREG_##ID_AA64##reg##_EL1: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 18 | |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 19 | #define SYSREG_READ(reg) \ |
| 20 | read_ID_AA64##reg##_EL1() |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 22 | #define SYSREG_READ_CLEAR(reg) \ |
| 23 | (read_ID_AA64##reg##_EL1() & \ |
| 24 | ~(ID_AA64##reg##_EL1_CLEAR)) |
| 25 | |
| 26 | #define SYSREG_READ_CLEAR_SET(reg) \ |
| 27 | ((read_ID_AA64##reg##_EL1() & \ |
| 28 | ~(ID_AA64##reg##_EL1_CLEAR)) | \ |
| 29 | (ID_AA64##reg##_EL1_SET)) |
| 30 | |
| 31 | /* System registers ID_AA64xxx_EL1 feature clear masks and set values */ |
| 32 | |
| 33 | /* |
| 34 | * ID_AA64DFR0_EL1: |
| 35 | * |
| 36 | * Cleared fields: |
| 37 | * - Debug architecture version: |
| 38 | * set in ID_AA64DFR0_EL1_SET |
| 39 | * - Trace unit System registers not implemented |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 40 | * - Number of breakpoints: |
| 41 | * set in ID_AA64DFR0_EL1_SET |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 42 | * - PMU Snapshot extension not implemented |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 43 | * - Number of watchpoints: |
| 44 | * set in ID_AA64DFR0_EL1_SET |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 45 | * - Synchronous-exception-based event profiling not implemented |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 46 | * - Number of breakpoints that are context-aware |
| 47 | * - Statistical Profiling Extension not implemented |
| 48 | * - Armv8.4 Self-hosted Trace Extension not implemented |
| 49 | * - Trace Buffer Extension not implemented |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 50 | * - Branch Record Buffer Extension not implemented |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 51 | * - Trace Buffer External Mode not implemented |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 52 | */ |
| 53 | #define ID_AA64DFR0_EL1_CLEAR \ |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 54 | MASK(ID_AA64DFR0_EL1_DebugVer) | \ |
| 55 | MASK(ID_AA64DFR0_EL1_TraceVer) | \ |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 56 | MASK(ID_AA64DFR0_EL1_BRPs) | \ |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 57 | MASK(ID_AA64DFR0_EL1_PMSS) | \ |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 58 | MASK(ID_AA64DFR0_EL1_WRPs) | \ |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 59 | MASK(ID_AA64DFR0_EL1_SEBEP) | \ |
AlexeiFedorov | 537bee0 | 2023-02-02 13:38:23 +0000 | [diff] [blame] | 60 | MASK(ID_AA64DFR0_EL1_CTX_CMPS) | \ |
| 61 | MASK(ID_AA64DFR0_EL1_PMSVer) | \ |
| 62 | MASK(ID_AA64DFR0_EL1_TraceFilt) | \ |
| 63 | MASK(ID_AA64DFR0_EL1_TraceBuffer) | \ |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 64 | MASK(ID_AA64DFR0_EL1_BRBE) | \ |
| 65 | MASK(ID_AA64DFR0_EL1_ExtTrcBuff) |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Set fields: |
| 69 | * - Armv8 debug architecture |
| 70 | * - Number of breakpoints: 2 |
| 71 | * - Number of watchpoints: 2 |
| 72 | */ |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 73 | #define ID_AA64DFR0_EL1_SET \ |
| 74 | INPLACE(ID_AA64DFR0_EL1_DebugVer, ID_AA64DFR0_EL1_Debugv8) | \ |
| 75 | INPLACE(ID_AA64DFR0_EL1_BRPs, 1UL) | \ |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 76 | INPLACE(ID_AA64DFR0_EL1_WRPs, 1UL) |
| 77 | |
| 78 | /* |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 79 | * ID_AA64DFR1_EL1: |
| 80 | * |
| 81 | * Cleared fields: |
| 82 | * - Exception-based event profiling not implemented |
| 83 | * - PMU fixed-function instruction counter not implemented |
| 84 | */ |
| 85 | #define ID_AA64DFR1_EL1_CLEAR \ |
| 86 | MASK(ID_AA64DFR1_EL1_EBEP) | \ |
| 87 | MASK(ID_AA64DFR1_EL1_ICNTR) |
| 88 | |
| 89 | /* |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 90 | * ID_AA64PFR0_EL1: |
| 91 | * |
| 92 | * Cleared fields: |
| 93 | * - Activity Monitors Extension not implemented |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 94 | */ |
| 95 | #define ID_AA64PFR0_EL1_CLEAR \ |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 96 | MASK(ID_AA64PFR0_EL1_AMU) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 97 | /* |
Arunachalam Ganapathy | a27de37 | 2023-03-06 11:13:49 +0000 | [diff] [blame] | 98 | * ID_AA64PFR1_EL1: |
| 99 | * |
| 100 | * Cleared fields: |
| 101 | * - Memory Tagging Extension is not implemented |
| 102 | */ |
| 103 | #define ID_AA64PFR1_EL1_CLEAR \ |
| 104 | MASK(ID_AA64PFR1_EL1_MTE) |
| 105 | |
| 106 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 107 | * Handle ID_AA64XXX<n>_EL1 instructions |
| 108 | */ |
| 109 | static bool handle_id_sysreg_trap(struct rec *rec, |
| 110 | struct rmi_rec_exit *rec_exit, |
| 111 | unsigned long esr) |
| 112 | { |
| 113 | unsigned int rt; |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 114 | unsigned long idreg, value; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * We only set HCR_EL2.TID3 to trap ID registers at the moment and |
| 118 | * that only traps reads of registers. Seeing a write here indicates a |
| 119 | * consistency problem with the RMM and we should panic immediately. |
| 120 | */ |
| 121 | assert(!ESR_EL2_SYSREG_IS_WRITE(esr)); |
| 122 | |
| 123 | /* |
| 124 | * Read Rt value from the issued instruction, |
| 125 | * the general-purpose register used for the transfer. |
AlexeiFedorov | feaef16 | 2022-12-23 16:59:51 +0000 | [diff] [blame] | 126 | * Rt bits [9:5] of ISS field cannot exceed 0b11111. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 127 | */ |
| 128 | rt = ESR_EL2_SYSREG_ISS_RT(esr); |
| 129 | |
| 130 | /* Handle writes to XZR register */ |
| 131 | if (rt == 31U) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | idreg = esr & ESR_EL2_SYSREG_MASK; |
| 136 | |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 137 | switch (idreg) { |
| 138 | SYSREG_CASE(AFR0) |
| 139 | value = SYSREG_READ(AFR0); |
| 140 | break; |
| 141 | SYSREG_CASE(AFR1) |
| 142 | value = SYSREG_READ(AFR1); |
| 143 | break; |
| 144 | SYSREG_CASE(DFR0) |
| 145 | value = SYSREG_READ_CLEAR_SET(DFR0); |
| 146 | break; |
| 147 | SYSREG_CASE(DFR1) |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 148 | value = SYSREG_READ_CLEAR(DFR1); |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 149 | break; |
| 150 | SYSREG_CASE(ISAR0) |
| 151 | value = SYSREG_READ(ISAR0); |
| 152 | break; |
| 153 | SYSREG_CASE(ISAR1) |
Arvind Ram Prakash | bd36a1b | 2022-12-15 12:16:36 -0600 | [diff] [blame] | 154 | value = SYSREG_READ(ISAR1); |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 155 | break; |
| 156 | SYSREG_CASE(MMFR0) |
| 157 | value = SYSREG_READ(MMFR0); |
| 158 | break; |
| 159 | SYSREG_CASE(MMFR1) |
| 160 | value = SYSREG_READ(MMFR1); |
| 161 | break; |
| 162 | SYSREG_CASE(MMFR2) |
| 163 | value = SYSREG_READ(MMFR2); |
| 164 | break; |
| 165 | SYSREG_CASE(PFR0) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 166 | /* |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 167 | * Workaround for TF-A trapping AMU registers access |
| 168 | * to EL3 in Realm state. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 169 | */ |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 170 | value = SYSREG_READ_CLEAR(PFR0); |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Clear SVE bits if architecture supports it but SVE is |
| 174 | * disabled for current realm. |
| 175 | */ |
| 176 | if ((EXTRACT(ID_AA64PFR0_EL1_SVE, value) != 0UL) && |
| 177 | rec->realm_info.sve_enabled == false) { |
| 178 | value &= ~MASK(ID_AA64PFR0_EL1_SVE); |
| 179 | } |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 180 | break; |
| 181 | SYSREG_CASE(PFR1) |
Arunachalam Ganapathy | a27de37 | 2023-03-06 11:13:49 +0000 | [diff] [blame] | 182 | value = SYSREG_READ_CLEAR(PFR1); |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 183 | break; |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 184 | SYSREG_CASE(ZFR0) |
| 185 | if (is_feat_sve_present() && rec->realm_info.sve_enabled) { |
| 186 | value = read_id_aa64zfr0_el1(); |
| 187 | } else { |
| 188 | value = 0UL; |
| 189 | } |
| 190 | break; |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 191 | default: |
| 192 | /* All other encodings are in the RES0 space */ |
| 193 | value = 0UL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 194 | } |
| 195 | |
AlexeiFedorov | 7bb7a70 | 2023-01-17 17:04:14 +0000 | [diff] [blame] | 196 | rec->regs[rt] = value; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
| 200 | static bool handle_icc_el1_sysreg_trap(struct rec *rec, |
| 201 | struct rmi_rec_exit *rec_exit, |
| 202 | unsigned long esr) |
| 203 | { |
| 204 | __unused unsigned long sysreg = esr & ESR_EL2_SYSREG_MASK; |
| 205 | |
| 206 | /* |
| 207 | * We should only have configured ICH_HCR_EL2 to trap on DIR and we |
| 208 | * always trap on the SGIRs following the architecture, so make sure |
| 209 | * we're not accidentally trapping on some other register here. |
| 210 | */ |
| 211 | assert((sysreg == ESR_EL2_SYSREG_ICC_DIR) || |
| 212 | (sysreg == ESR_EL2_SYSREG_ICC_SGI1R_EL1) || |
| 213 | (sysreg == ESR_EL2_SYSREG_ICC_SGI0R_EL1)); |
| 214 | |
| 215 | /* |
| 216 | * The registers above should only trap to EL2 for writes, read |
| 217 | * instructions are not defined and should cause an Undefined exception |
| 218 | * at EL1. |
| 219 | */ |
| 220 | assert(ESR_EL2_SYSREG_IS_WRITE(esr)); |
| 221 | |
| 222 | rec_exit->exit_reason = RMI_EXIT_SYNC; |
| 223 | rec_exit->esr = esr; |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | typedef bool (*sysreg_handler_fn)(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 228 | unsigned long esr); |
| 229 | |
| 230 | struct sysreg_handler { |
| 231 | unsigned long esr_mask; |
| 232 | unsigned long esr_value; |
| 233 | sysreg_handler_fn fn; |
| 234 | }; |
| 235 | |
| 236 | #define SYSREG_HANDLER(_mask, _value, _handler_fn) \ |
| 237 | { .esr_mask = (_mask), .esr_value = (_value), .fn = _handler_fn } |
| 238 | |
| 239 | static const struct sysreg_handler sysreg_handlers[] = { |
| 240 | SYSREG_HANDLER(ESR_EL2_SYSREG_ID_MASK, ESR_EL2_SYSREG_ID, handle_id_sysreg_trap), |
| 241 | SYSREG_HANDLER(ESR_EL2_SYSREG_ICC_EL1_MASK, ESR_EL2_SYSREG_ICC_EL1, handle_icc_el1_sysreg_trap), |
| 242 | SYSREG_HANDLER(ESR_EL2_SYSREG_MASK, ESR_EL2_SYSREG_ICC_PMR_EL1, handle_icc_el1_sysreg_trap) |
| 243 | }; |
| 244 | |
| 245 | static unsigned long get_sysreg_write_value(struct rec *rec, unsigned long esr) |
| 246 | { |
AlexeiFedorov | feaef16 | 2022-12-23 16:59:51 +0000 | [diff] [blame] | 247 | /* Rt bits [9:5] of ISS field cannot exceed 0b11111 */ |
| 248 | unsigned int rt = ESR_EL2_SYSREG_ISS_RT(esr); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 249 | |
| 250 | /* Handle reads from XZR register */ |
| 251 | if (rt == 31U) { |
| 252 | return 0UL; |
| 253 | } |
| 254 | |
AlexeiFedorov | feaef16 | 2022-12-23 16:59:51 +0000 | [diff] [blame] | 255 | return rec->regs[rt]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static void emulate_sysreg_access_ns(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 259 | unsigned long esr) |
| 260 | { |
| 261 | if (ESR_EL2_SYSREG_IS_WRITE(esr)) { |
| 262 | rec_exit->gprs[0] = get_sysreg_write_value(rec, esr); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Handle trapped MSR, MRS or System instruction execution |
| 268 | * in AArch64 state |
| 269 | */ |
| 270 | bool handle_sysreg_access_trap(struct rec *rec, struct rmi_rec_exit *rec_exit, |
| 271 | unsigned long esr) |
| 272 | { |
| 273 | /* |
| 274 | * Read Rt value from the issued instruction, |
| 275 | * the general-purpose register used for the transfer. |
AlexeiFedorov | feaef16 | 2022-12-23 16:59:51 +0000 | [diff] [blame] | 276 | * Rt bits [9:5] of ISS field cannot exceed 0b11111. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 277 | */ |
| 278 | unsigned int rt = ESR_EL2_SYSREG_ISS_RT(esr); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 279 | unsigned int __unused op0, op1, crn, crm, op2; |
| 280 | unsigned long __unused sysreg; |
| 281 | |
| 282 | /* Check for 32-bit instruction trapped */ |
| 283 | assert(ESR_IL(esr) != 0UL); |
| 284 | |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 285 | for (unsigned int i = 0U; i < ARRAY_LEN(sysreg_handlers); i++) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 286 | const struct sysreg_handler *handler = &sysreg_handlers[i]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 287 | |
| 288 | if ((esr & handler->esr_mask) == handler->esr_value) { |
Shruti Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 289 | bool handled = handler->fn(rec, rec_exit, esr); |
| 290 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 291 | if (!handled) { |
| 292 | emulate_sysreg_access_ns(rec, rec_exit, esr); |
| 293 | } |
| 294 | return handled; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * For now, treat all unhandled accesses as RAZ/WI. |
| 300 | * Handle writes to XZR register. |
| 301 | */ |
| 302 | if (!ESR_EL2_SYSREG_IS_WRITE(esr) && (rt != 31U)) { |
AlexeiFedorov | feaef16 | 2022-12-23 16:59:51 +0000 | [diff] [blame] | 303 | rec->regs[rt] = 0UL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | sysreg = esr & ESR_EL2_SYSREG_MASK; |
| 307 | |
| 308 | /* Extract sytem register encoding */ |
| 309 | op0 = EXTRACT(ESR_EL2_SYSREG_TRAP_OP0, sysreg); |
| 310 | op1 = EXTRACT(ESR_EL2_SYSREG_TRAP_OP1, sysreg); |
| 311 | crn = EXTRACT(ESR_EL2_SYSREG_TRAP_CRN, sysreg); |
| 312 | crm = EXTRACT(ESR_EL2_SYSREG_TRAP_CRM, sysreg); |
| 313 | op2 = EXTRACT(ESR_EL2_SYSREG_TRAP_OP2, sysreg); |
| 314 | |
| 315 | INFO("Unhandled %s S%u_%u_C%u_C%u_%u\n", |
| 316 | ESR_EL2_SYSREG_IS_WRITE(esr) ? "write" : "read", |
| 317 | op0, op1, crn, crm, op2); |
| 318 | |
| 319 | return true; |
| 320 | } |