Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Manish Pandey | 067087f | 2023-12-08 20:13:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <platform_def.h> |
| 8 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 9 | #include <arch.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 10 | #include <asm_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <bl31/ea_handle.h> |
| 12 | #include <bl31/interrupt_mgmt.h> |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 13 | #include <bl31/sync_handle.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <common/runtime_svc.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 15 | #include <context.h> |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 16 | #include <cpu_macros.S> |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 17 | #include <el3_common_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | #include <lib/el3_runtime/cpu_data.h> |
| 19 | #include <lib/smccc.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 20 | |
| 21 | .globl runtime_exceptions |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 22 | |
Dimitris Papastamos | 446f7f1 | 2017-11-30 14:53:53 +0000 | [diff] [blame] | 23 | .globl sync_exception_sp_el0 |
| 24 | .globl irq_sp_el0 |
| 25 | .globl fiq_sp_el0 |
| 26 | .globl serror_sp_el0 |
| 27 | |
| 28 | .globl sync_exception_sp_elx |
| 29 | .globl irq_sp_elx |
| 30 | .globl fiq_sp_elx |
| 31 | .globl serror_sp_elx |
| 32 | |
| 33 | .globl sync_exception_aarch64 |
| 34 | .globl irq_aarch64 |
| 35 | .globl fiq_aarch64 |
| 36 | .globl serror_aarch64 |
| 37 | |
| 38 | .globl sync_exception_aarch32 |
| 39 | .globl irq_aarch32 |
| 40 | .globl fiq_aarch32 |
| 41 | .globl serror_aarch32 |
| 42 | |
Jeenu Viswambharan | 96c7df0 | 2017-11-30 12:54:15 +0000 | [diff] [blame] | 43 | /* |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 44 | * Save LR and make x30 available as most of the routines in vector entry |
| 45 | * need a free register |
| 46 | */ |
| 47 | .macro save_x30 |
| 48 | str x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] |
| 49 | .endm |
| 50 | |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 51 | .macro restore_x30 |
| 52 | ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] |
| 53 | .endm |
Madhukar Pappireddy | fba2572 | 2020-07-24 03:27:12 -0500 | [diff] [blame] | 54 | |
Madhukar Pappireddy | fba2572 | 2020-07-24 03:27:12 -0500 | [diff] [blame] | 55 | /* |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 56 | * Macro that synchronizes errors (EA) and checks for pending SError. |
| 57 | * On detecting a pending SError it either reflects it back to lower |
| 58 | * EL (KFH) or handles it in EL3 (FFH) based on EA routing model. |
Madhukar Pappireddy | fba2572 | 2020-07-24 03:27:12 -0500 | [diff] [blame] | 59 | */ |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 60 | .macro sync_and_handle_pending_serror |
Manish Pandey | 6b5721f | 2023-06-26 17:46:14 +0100 | [diff] [blame] | 61 | synchronize_errors |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 62 | mrs x30, ISR_EL1 |
| 63 | tbz x30, #ISR_A_SHIFT, 2f |
Manish Pandey | f90a73c | 2023-10-10 15:42:19 +0100 | [diff] [blame] | 64 | #if FFH_SUPPORT |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 65 | mrs x30, scr_el3 |
| 66 | tst x30, #SCR_EA_BIT |
| 67 | b.eq 1f |
| 68 | bl handle_pending_async_ea |
| 69 | b 2f |
Madhukar Pappireddy | fba2572 | 2020-07-24 03:27:12 -0500 | [diff] [blame] | 70 | #endif |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 71 | 1: |
| 72 | /* This function never returns, but need LR for decision making */ |
| 73 | bl reflect_pending_async_ea_to_lower_el |
| 74 | 2: |
Manish Pandey | b3c6198 | 2023-01-06 13:38:03 +0000 | [diff] [blame] | 75 | .endm |
Madhukar Pappireddy | fba2572 | 2020-07-24 03:27:12 -0500 | [diff] [blame] | 76 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 77 | /* --------------------------------------------------------------------- |
| 78 | * This macro handles Synchronous exceptions. |
| 79 | * Only SMC exceptions are supported. |
| 80 | * --------------------------------------------------------------------- |
Achin Gupta | 9cf2bb7 | 2014-05-09 11:07:09 +0100 | [diff] [blame] | 81 | */ |
| 82 | .macro handle_sync_exception |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 83 | #if ENABLE_RUNTIME_INSTRUMENTATION |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 84 | /* |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 85 | * Read the timestamp value and store it in per-cpu data. The value |
| 86 | * will be extracted from per-cpu data by the C level SMC handler and |
| 87 | * saved to the PMF timestamp region. |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 88 | */ |
| 89 | mrs x30, cntpct_el0 |
| 90 | str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] |
| 91 | mrs x29, tpidr_el3 |
| 92 | str x30, [x29, #CPU_DATA_PMF_TS0_OFFSET] |
| 93 | ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] |
| 94 | #endif |
| 95 | |
Achin Gupta | 9cf2bb7 | 2014-05-09 11:07:09 +0100 | [diff] [blame] | 96 | mrs x30, esr_el3 |
| 97 | ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 98 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 99 | /* Handle SMC exceptions separately from other synchronous exceptions */ |
Achin Gupta | 9cf2bb7 | 2014-05-09 11:07:09 +0100 | [diff] [blame] | 100 | cmp x30, #EC_AARCH32_SMC |
| 101 | b.eq smc_handler32 |
| 102 | |
| 103 | cmp x30, #EC_AARCH64_SMC |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 104 | b.eq sync_handler64 |
| 105 | |
| 106 | cmp x30, #EC_AARCH64_SYS |
| 107 | b.eq sync_handler64 |
Achin Gupta | 9cf2bb7 | 2014-05-09 11:07:09 +0100 | [diff] [blame] | 108 | |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 109 | cmp x30, #EC_IMP_DEF_EL3 |
| 110 | b.eq imp_def_el3_handler |
| 111 | |
| 112 | /* If FFH Support then try to handle lower EL EA exceptions. */ |
| 113 | #if FFH_SUPPORT |
| 114 | mrs x30, scr_el3 |
| 115 | tst x30, #SCR_EA_BIT |
| 116 | b.eq 1f |
Manish Pandey | c918c18 | 2023-01-11 21:53:02 +0000 | [diff] [blame] | 117 | b handle_lower_el_sync_ea |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 118 | #endif |
| 119 | 1: |
| 120 | /* Synchronous exceptions other than the above are unhandled */ |
| 121 | b report_unhandled_exception |
Achin Gupta | 9cf2bb7 | 2014-05-09 11:07:09 +0100 | [diff] [blame] | 122 | .endm |
| 123 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 124 | vector_base runtime_exceptions |
| 125 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 126 | /* --------------------------------------------------------------------- |
| 127 | * Current EL with SP_EL0 : 0x0 - 0x200 |
| 128 | * --------------------------------------------------------------------- |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 129 | */ |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 130 | vector_entry sync_exception_sp_el0 |
Justin Chadwell | 83e0488 | 2019-08-20 11:01:52 +0100 | [diff] [blame] | 131 | #ifdef MONITOR_TRAPS |
| 132 | stp x29, x30, [sp, #-16]! |
| 133 | |
| 134 | mrs x30, esr_el3 |
| 135 | ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 136 | |
| 137 | /* Check for BRK */ |
| 138 | cmp x30, #EC_BRK |
| 139 | b.eq brk_handler |
| 140 | |
| 141 | ldp x29, x30, [sp], #16 |
| 142 | #endif /* MONITOR_TRAPS */ |
| 143 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 144 | /* We don't expect any synchronous exceptions from EL3 */ |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 145 | b report_unhandled_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 146 | end_vector_entry sync_exception_sp_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 147 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 148 | vector_entry irq_sp_el0 |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 149 | /* |
| 150 | * EL3 code is non-reentrant. Any asynchronous exception is a serious |
| 151 | * error. Loop infinitely. |
| 152 | */ |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 153 | b report_unhandled_interrupt |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 154 | end_vector_entry irq_sp_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 155 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 156 | |
| 157 | vector_entry fiq_sp_el0 |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 158 | b report_unhandled_interrupt |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 159 | end_vector_entry fiq_sp_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 160 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 161 | |
| 162 | vector_entry serror_sp_el0 |
Jeenu Viswambharan | 911fcc9 | 2018-07-06 16:50:06 +0100 | [diff] [blame] | 163 | no_ret plat_handle_el3_ea |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 164 | end_vector_entry serror_sp_el0 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 165 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 166 | /* --------------------------------------------------------------------- |
| 167 | * Current EL with SP_ELx: 0x200 - 0x400 |
| 168 | * --------------------------------------------------------------------- |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 169 | */ |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 170 | vector_entry sync_exception_sp_elx |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 171 | /* |
| 172 | * This exception will trigger if anything went wrong during a previous |
| 173 | * exception entry or exit or while handling an earlier unexpected |
| 174 | * synchronous exception. There is a high probability that SP_EL3 is |
| 175 | * corrupted. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 176 | */ |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 177 | b report_unhandled_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 178 | end_vector_entry sync_exception_sp_elx |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 179 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 180 | vector_entry irq_sp_elx |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 181 | b report_unhandled_interrupt |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 182 | end_vector_entry irq_sp_elx |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 183 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 184 | vector_entry fiq_sp_elx |
Julius Werner | 67ebde7 | 2017-07-27 14:59:34 -0700 | [diff] [blame] | 185 | b report_unhandled_interrupt |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 186 | end_vector_entry fiq_sp_elx |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 187 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 188 | vector_entry serror_sp_elx |
Manish Pandey | f90a73c | 2023-10-10 15:42:19 +0100 | [diff] [blame] | 189 | #if FFH_SUPPORT |
Manish Pandey | b3c6198 | 2023-01-06 13:38:03 +0000 | [diff] [blame] | 190 | /* |
| 191 | * This will trigger if the exception was taken due to SError in EL3 or |
| 192 | * because of pending asynchronous external aborts from lower EL that got |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 193 | * triggered due to implicit/explicit synchronization in EL3 (SCR_EL3.EA=1) |
| 194 | * during EL3 entry. For the former case we continue with "plat_handle_el3_ea". |
| 195 | * The later case will occur when PSTATE.A bit is cleared in |
| 196 | * "handle_pending_async_ea". This means we are doing a nested |
| 197 | * exception in EL3. Call the handler for async EA which will eret back to |
| 198 | * original el3 handler if it is nested exception. Also, unmask EA so that we |
| 199 | * catch any further EA arise when handling this nested exception at EL3. |
Manish Pandey | b3c6198 | 2023-01-06 13:38:03 +0000 | [diff] [blame] | 200 | */ |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 201 | save_x30 |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 202 | ldr x30, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] |
| 203 | cbz x30, 1f |
| 204 | /* |
| 205 | * This is nested exception handling, clear the flag to avoid taking this |
| 206 | * path for further exceptions caused by EA handling |
| 207 | */ |
| 208 | str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] |
| 209 | unmask_async_ea |
Manish Pandey | b3c6198 | 2023-01-06 13:38:03 +0000 | [diff] [blame] | 210 | b handle_lower_el_async_ea |
| 211 | 1: |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 212 | restore_x30 |
Madhukar Pappireddy | fba2572 | 2020-07-24 03:27:12 -0500 | [diff] [blame] | 213 | #endif |
Jeenu Viswambharan | 911fcc9 | 2018-07-06 16:50:06 +0100 | [diff] [blame] | 214 | no_ret plat_handle_el3_ea |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 215 | |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 216 | end_vector_entry serror_sp_elx |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 217 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 218 | /* --------------------------------------------------------------------- |
Sandrine Bailleux | 046cd3f | 2014-08-06 11:27:23 +0100 | [diff] [blame] | 219 | * Lower EL using AArch64 : 0x400 - 0x600 |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 220 | * --------------------------------------------------------------------- |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 221 | */ |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 222 | vector_entry sync_exception_aarch64 |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 223 | /* |
| 224 | * This exception vector will be the entry point for SMCs and traps |
| 225 | * that are unhandled at lower ELs most commonly. SP_EL3 should point |
| 226 | * to a valid cpu context where the general purpose and system register |
| 227 | * state can be saved. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 228 | */ |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 229 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 230 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 231 | sync_and_handle_pending_serror |
| 232 | unmask_async_ea |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 233 | handle_sync_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 234 | end_vector_entry sync_exception_aarch64 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 235 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 236 | vector_entry irq_aarch64 |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 237 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 238 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 239 | sync_and_handle_pending_serror |
| 240 | unmask_async_ea |
Manish Pandey | 62040f4 | 2023-07-20 14:08:38 +0100 | [diff] [blame] | 241 | b handle_interrupt_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 242 | end_vector_entry irq_aarch64 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 243 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 244 | vector_entry fiq_aarch64 |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 245 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 246 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 247 | sync_and_handle_pending_serror |
| 248 | unmask_async_ea |
Manish Pandey | 62040f4 | 2023-07-20 14:08:38 +0100 | [diff] [blame] | 249 | b handle_interrupt_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 250 | end_vector_entry fiq_aarch64 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 251 | |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Need to synchronize any outstanding SError since we can get a burst of errors. |
| 254 | * So reuse the sync mechanism to catch any further errors which are pending. |
| 255 | */ |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 256 | vector_entry serror_aarch64 |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 257 | #if FFH_SUPPORT |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 258 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 259 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 260 | sync_and_handle_pending_serror |
| 261 | unmask_async_ea |
Manish Pandey | c918c18 | 2023-01-11 21:53:02 +0000 | [diff] [blame] | 262 | b handle_lower_el_async_ea |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 263 | #else |
| 264 | b report_unhandled_exception |
| 265 | #endif |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 266 | end_vector_entry serror_aarch64 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 267 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 268 | /* --------------------------------------------------------------------- |
Sandrine Bailleux | 046cd3f | 2014-08-06 11:27:23 +0100 | [diff] [blame] | 269 | * Lower EL using AArch32 : 0x600 - 0x800 |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 270 | * --------------------------------------------------------------------- |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 271 | */ |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 272 | vector_entry sync_exception_aarch32 |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 273 | /* |
| 274 | * This exception vector will be the entry point for SMCs and traps |
| 275 | * that are unhandled at lower ELs most commonly. SP_EL3 should point |
| 276 | * to a valid cpu context where the general purpose and system register |
| 277 | * state can be saved. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 278 | */ |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 279 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 280 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 281 | sync_and_handle_pending_serror |
| 282 | unmask_async_ea |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 283 | handle_sync_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 284 | end_vector_entry sync_exception_aarch32 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 285 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 286 | vector_entry irq_aarch32 |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 287 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 288 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 289 | sync_and_handle_pending_serror |
| 290 | unmask_async_ea |
Manish Pandey | 62040f4 | 2023-07-20 14:08:38 +0100 | [diff] [blame] | 291 | b handle_interrupt_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 292 | end_vector_entry irq_aarch32 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 293 | |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 294 | vector_entry fiq_aarch32 |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 295 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 296 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 297 | sync_and_handle_pending_serror |
| 298 | unmask_async_ea |
Manish Pandey | 62040f4 | 2023-07-20 14:08:38 +0100 | [diff] [blame] | 299 | b handle_interrupt_exception |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 300 | end_vector_entry fiq_aarch32 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 301 | |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 302 | /* |
| 303 | * Need to synchronize any outstanding SError since we can get a burst of errors. |
| 304 | * So reuse the sync mechanism to catch any further errors which are pending. |
| 305 | */ |
Sandrine Bailleux | 9e6ad6c | 2016-05-24 16:56:03 +0100 | [diff] [blame] | 306 | vector_entry serror_aarch32 |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 307 | #if FFH_SUPPORT |
Manish Pandey | 66a056e | 2023-01-11 21:41:07 +0000 | [diff] [blame] | 308 | save_x30 |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 309 | apply_at_speculative_wa |
Manish Pandey | 07952fb | 2023-05-25 13:46:14 +0100 | [diff] [blame] | 310 | sync_and_handle_pending_serror |
| 311 | unmask_async_ea |
Manish Pandey | c918c18 | 2023-01-11 21:53:02 +0000 | [diff] [blame] | 312 | b handle_lower_el_async_ea |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 313 | #else |
| 314 | b report_unhandled_exception |
| 315 | #endif |
Roberto Vargas | 95f30ab | 2018-04-17 11:31:43 +0100 | [diff] [blame] | 316 | end_vector_entry serror_aarch32 |
Jeenu Viswambharan | a7934d6 | 2014-02-07 15:53:18 +0000 | [diff] [blame] | 317 | |
Justin Chadwell | 83e0488 | 2019-08-20 11:01:52 +0100 | [diff] [blame] | 318 | #ifdef MONITOR_TRAPS |
| 319 | .section .rodata.brk_string, "aS" |
| 320 | brk_location: |
| 321 | .asciz "Error at instruction 0x" |
| 322 | brk_message: |
| 323 | .asciz "Unexpected BRK instruction with value 0x" |
| 324 | #endif /* MONITOR_TRAPS */ |
| 325 | |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 326 | /* --------------------------------------------------------------------- |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 327 | * The following code handles secure monitor calls. |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 328 | * Depending upon the execution state from where the SMC has been |
| 329 | * invoked, it frees some general purpose registers to perform the |
| 330 | * remaining tasks. They involve finding the runtime service handler |
| 331 | * that is the target of the SMC & switching to runtime stacks (SP_EL0) |
| 332 | * before calling the handler. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 333 | * |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 334 | * Note that x30 has been explicitly saved and can be used here |
| 335 | * --------------------------------------------------------------------- |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 336 | */ |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 337 | func sync_exception_handler |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 338 | smc_handler32: |
| 339 | /* Check whether aarch32 issued an SMC64 */ |
| 340 | tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited |
| 341 | |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 342 | sync_handler64: |
Antonio Nino Diaz | 594811b | 2019-01-31 11:58:00 +0000 | [diff] [blame] | 343 | /* NOTE: The code below must preserve x0-x4 */ |
| 344 | |
Alexei Fedorov | 503bbf3 | 2019-08-13 15:17:53 +0100 | [diff] [blame] | 345 | /* |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 346 | * Save general purpose and ARMv8.3-PAuth registers (if enabled). |
Boyan Karatotev | ed85cf7 | 2022-12-06 09:03:42 +0000 | [diff] [blame] | 347 | * Also save PMCR_EL0 and set the PSTATE to a known state. |
Alexei Fedorov | 503bbf3 | 2019-08-13 15:17:53 +0100 | [diff] [blame] | 348 | */ |
Daniel Boulby | 95fb1aa | 2022-01-19 11:20:05 +0000 | [diff] [blame] | 349 | bl prepare_el3_entry |
Alexei Fedorov | 503bbf3 | 2019-08-13 15:17:53 +0100 | [diff] [blame] | 350 | |
Antonio Nino Diaz | 25cda67 | 2019-02-19 11:53:51 +0000 | [diff] [blame] | 351 | #if ENABLE_PAUTH |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 352 | /* Load and program APIAKey firmware key */ |
| 353 | bl pauth_load_bl31_apiakey |
Antonio Nino Diaz | 25cda67 | 2019-02-19 11:53:51 +0000 | [diff] [blame] | 354 | #endif |
Antonio Nino Diaz | 594811b | 2019-01-31 11:58:00 +0000 | [diff] [blame] | 355 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 356 | /* |
| 357 | * Populate the parameters for the SMC handler. |
| 358 | * We already have x0-x4 in place. x5 will point to a cookie (not used |
| 359 | * now). x6 will point to the context structure (SP_EL3) and x7 will |
Dimitris Papastamos | 0415951 | 2018-01-22 11:53:04 +0000 | [diff] [blame] | 360 | * contain flags we need to pass to the handler. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 361 | */ |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 362 | mov x5, xzr |
| 363 | mov x6, sp |
| 364 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 365 | /* |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 366 | * Restore the saved C runtime stack value which will become the new |
| 367 | * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context' |
| 368 | * structure prior to the last ERET from EL3. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 369 | */ |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 370 | ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] |
| 371 | |
| 372 | /* Switch to SP_EL0 */ |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 373 | msr spsel, #MODE_SP_EL0 |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 374 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 375 | /* |
Manish Pandey | 70bbdbd | 2022-12-07 13:04:20 +0000 | [diff] [blame] | 376 | * Save the SPSR_EL3 and ELR_EL3 in case there is a world |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 377 | * switch during SMC handling. |
| 378 | * TODO: Revisit if all system registers can be saved later. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 379 | */ |
| 380 | mrs x16, spsr_el3 |
| 381 | mrs x17, elr_el3 |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 382 | stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] |
Manish Pandey | 70bbdbd | 2022-12-07 13:04:20 +0000 | [diff] [blame] | 383 | |
| 384 | /* Load SCR_EL3 */ |
| 385 | mrs x18, scr_el3 |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 386 | |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 387 | /* check for system register traps */ |
| 388 | mrs x16, esr_el3 |
| 389 | ubfx x17, x16, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 390 | cmp x17, #EC_AARCH64_SYS |
| 391 | b.eq sysreg_handler64 |
| 392 | |
Zelalem Aweke | 4d666ac | 2021-07-08 17:13:09 -0500 | [diff] [blame] | 393 | /* Clear flag register */ |
| 394 | mov x7, xzr |
| 395 | |
| 396 | #if ENABLE_RME |
| 397 | /* Copy SCR_EL3.NSE bit to the flag to indicate caller's security */ |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 398 | ubfx x7, x18, #SCR_NSE_SHIFT, #1 |
Zelalem Aweke | 4d666ac | 2021-07-08 17:13:09 -0500 | [diff] [blame] | 399 | |
| 400 | /* |
| 401 | * Shift copied SCR_EL3.NSE bit by 5 to create space for |
Olivier Deprez | 33dd845 | 2022-10-11 15:38:27 +0200 | [diff] [blame] | 402 | * SCR_EL3.NS bit. Bit 5 of the flag corresponds to |
Zelalem Aweke | 4d666ac | 2021-07-08 17:13:09 -0500 | [diff] [blame] | 403 | * the SCR_EL3.NSE bit. |
| 404 | */ |
| 405 | lsl x7, x7, #5 |
| 406 | #endif /* ENABLE_RME */ |
| 407 | |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 408 | /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */ |
| 409 | bfi x7, x18, #0, #1 |
| 410 | |
Jayanth Dodderi Chidanand | 3e474f7 | 2023-03-09 13:56:03 +0000 | [diff] [blame] | 411 | mov sp, x12 |
| 412 | |
| 413 | /* |
| 414 | * Per SMCCC documentation, bits [23:17] must be zero for Fast |
| 415 | * SMCs. Other values are reserved for future use. Ensure that |
| 416 | * these bits are zeroes, if not report as unknown SMC. |
| 417 | */ |
| 418 | tbz x0, #FUNCID_TYPE_SHIFT, 2f /* Skip check if its a Yield Call*/ |
| 419 | tst x0, #(FUNCID_FC_RESERVED_MASK << FUNCID_FC_RESERVED_SHIFT) |
| 420 | b.ne smc_unknown |
| 421 | |
Olivier Deprez | 33dd845 | 2022-10-11 15:38:27 +0200 | [diff] [blame] | 422 | /* |
| 423 | * Per SMCCCv1.3 a caller can set the SVE hint bit in the SMC FID |
| 424 | * passed through x0. Copy the SVE hint bit to flags and mask the |
| 425 | * bit in smc_fid passed to the standard service dispatcher. |
| 426 | * A service/dispatcher can retrieve the SVE hint bit state from |
| 427 | * flags using the appropriate helper. |
| 428 | */ |
Jayanth Dodderi Chidanand | 3e474f7 | 2023-03-09 13:56:03 +0000 | [diff] [blame] | 429 | 2: |
Olivier Deprez | 62cc109 | 2023-05-24 17:42:00 +0200 | [diff] [blame] | 430 | and x16, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) |
| 431 | orr x7, x7, x16 |
Olivier Deprez | 33dd845 | 2022-10-11 15:38:27 +0200 | [diff] [blame] | 432 | bic x0, x0, #(FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT) |
| 433 | |
Madhukar Pappireddy | d87233a | 2019-05-08 15:41:41 -0500 | [diff] [blame] | 434 | /* Get the unique owning entity number */ |
| 435 | ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH |
| 436 | ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH |
| 437 | orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH |
| 438 | |
| 439 | /* Load descriptor index from array of indices */ |
Madhukar Pappireddy | f4e6ea6 | 2020-01-27 15:32:15 -0600 | [diff] [blame] | 440 | adrp x14, rt_svc_descs_indices |
| 441 | add x14, x14, :lo12:rt_svc_descs_indices |
Madhukar Pappireddy | d87233a | 2019-05-08 15:41:41 -0500 | [diff] [blame] | 442 | ldrb w15, [x14, x16] |
| 443 | |
| 444 | /* Any index greater than 127 is invalid. Check bit 7. */ |
| 445 | tbnz w15, 7, smc_unknown |
| 446 | |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 447 | /* |
Madhukar Pappireddy | d87233a | 2019-05-08 15:41:41 -0500 | [diff] [blame] | 448 | * Get the descriptor using the index |
| 449 | * x11 = (base + off), w15 = index |
| 450 | * |
| 451 | * handler = (base + off) + (index << log2(size)) |
| 452 | */ |
developer | ff69a6a | 2024-02-21 13:58:51 +0800 | [diff] [blame] | 453 | adr_l x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE) |
Madhukar Pappireddy | d87233a | 2019-05-08 15:41:41 -0500 | [diff] [blame] | 454 | lsl w10, w15, #RT_SVC_SIZE_LOG2 |
| 455 | ldr x15, [x11, w10, uxtw] |
| 456 | |
| 457 | /* |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 458 | * Call the Secure Monitor Call handler and then drop directly into |
| 459 | * el3_exit() which will program any remaining architectural state |
| 460 | * prior to issuing the ERET to the desired lower EL. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 461 | */ |
| 462 | #if DEBUG |
| 463 | cbz x15, rt_svc_fw_critical_error |
| 464 | #endif |
| 465 | blr x15 |
| 466 | |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 467 | b el3_exit |
| 468 | |
| 469 | sysreg_handler64: |
| 470 | mov x0, x16 /* ESR_EL3, containing syndrome information */ |
| 471 | mov x1, x6 /* lower EL's context */ |
| 472 | mov x19, x6 /* save context pointer for after the call */ |
| 473 | mov sp, x12 /* EL3 runtime stack, as loaded above */ |
| 474 | |
| 475 | /* int handle_sysreg_trap(uint64_t esr_el3, cpu_context_t *ctx); */ |
| 476 | bl handle_sysreg_trap |
| 477 | /* |
| 478 | * returns: |
Manish Pandey | 067087f | 2023-12-08 20:13:29 +0000 | [diff] [blame] | 479 | * -1: unhandled trap, UNDEF injection into lower EL |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 480 | * 0: handled trap, return to the trapping instruction (repeating it) |
| 481 | * 1: handled trap, return to the next instruction |
| 482 | */ |
| 483 | |
| 484 | tst w0, w0 |
Manish Pandey | 067087f | 2023-12-08 20:13:29 +0000 | [diff] [blame] | 485 | b.mi 2f /* negative: undefined exception injection */ |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 486 | |
Manish Pandey | 067087f | 2023-12-08 20:13:29 +0000 | [diff] [blame] | 487 | b.eq 1f /* zero: do not change ELR_EL3 */ |
| 488 | /* positive: advance the PC to continue after the instruction */ |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 489 | ldr x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] |
| 490 | add x1, x1, #4 |
| 491 | str x1, [x19, #CTX_EL3STATE_OFFSET + CTX_ELR_EL3] |
| 492 | 1: |
Yatharth Kochar | 6c0566c | 2015-10-02 17:56:48 +0100 | [diff] [blame] | 493 | b el3_exit |
Manish Pandey | 067087f | 2023-12-08 20:13:29 +0000 | [diff] [blame] | 494 | 2: |
| 495 | /* |
| 496 | * UNDEF injection to lower EL, the support is only provided for lower |
| 497 | * EL in AArch64 mode, for AArch32 mode it will do elx_panic as before. |
| 498 | */ |
| 499 | mrs x0, spsr_el3 |
| 500 | tst x0, #(SPSR_M_MASK << SPSR_M_SHIFT) |
| 501 | b.ne elx_panic |
| 502 | /* Pass context pointer as an argument to inject_undef64 */ |
| 503 | mov x0, x19 |
| 504 | bl inject_undef64 |
| 505 | b el3_exit |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 506 | |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 507 | smc_unknown: |
| 508 | /* |
Madhukar Pappireddy | d87233a | 2019-05-08 15:41:41 -0500 | [diff] [blame] | 509 | * Unknown SMC call. Populate return value with SMC_UNK and call |
| 510 | * el3_exit() which will restore the remaining architectural state |
| 511 | * i.e., SYS, GP and PAuth registers(if any) prior to issuing the ERET |
Jayanth Dodderi Chidanand | 3e474f7 | 2023-03-09 13:56:03 +0000 | [diff] [blame] | 512 | * to the desired lower EL. |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 513 | */ |
Antonio Nino Diaz | e4794b7 | 2018-02-14 14:22:29 +0000 | [diff] [blame] | 514 | mov x0, #SMC_UNK |
Madhukar Pappireddy | d87233a | 2019-05-08 15:41:41 -0500 | [diff] [blame] | 515 | str x0, [x6, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] |
| 516 | b el3_exit |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 517 | |
| 518 | smc_prohibited: |
Manish V Badarkhe | e07e808 | 2020-07-23 12:43:25 +0100 | [diff] [blame] | 519 | restore_ptw_el1_sys_regs |
| 520 | ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] |
Soby Mathew | 6c5192a | 2014-04-30 15:36:37 +0100 | [diff] [blame] | 521 | ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] |
Antonio Nino Diaz | e4794b7 | 2018-02-14 14:22:29 +0000 | [diff] [blame] | 522 | mov x0, #SMC_UNK |
Anthony Steinhauser | 0f7e601 | 2020-01-07 15:44:06 -0800 | [diff] [blame] | 523 | exception_return |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 524 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 525 | #if DEBUG |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 526 | rt_svc_fw_critical_error: |
Douglas Raillard | 0980eed | 2016-11-09 17:48:27 +0000 | [diff] [blame] | 527 | /* Switch to SP_ELx */ |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 528 | msr spsel, #MODE_SP_ELX |
Jeenu Viswambharan | 68aef10 | 2016-11-30 15:21:11 +0000 | [diff] [blame] | 529 | no_ret report_unhandled_exception |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 530 | #endif |
Andre Przywara | fa914d8 | 2022-11-21 17:04:10 +0000 | [diff] [blame] | 531 | endfunc sync_exception_handler |
Justin Chadwell | 83e0488 | 2019-08-20 11:01:52 +0100 | [diff] [blame] | 532 | |
| 533 | /* --------------------------------------------------------------------- |
Manish Pandey | 62040f4 | 2023-07-20 14:08:38 +0100 | [diff] [blame] | 534 | * This function handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS |
| 535 | * interrupts. |
| 536 | * |
| 537 | * Note that x30 has been explicitly saved and can be used here |
| 538 | * --------------------------------------------------------------------- |
| 539 | */ |
| 540 | func handle_interrupt_exception |
| 541 | /* |
| 542 | * Save general purpose and ARMv8.3-PAuth registers (if enabled). |
| 543 | * Also save PMCR_EL0 and set the PSTATE to a known state. |
| 544 | */ |
| 545 | bl prepare_el3_entry |
| 546 | |
| 547 | #if ENABLE_PAUTH |
| 548 | /* Load and program APIAKey firmware key */ |
| 549 | bl pauth_load_bl31_apiakey |
| 550 | #endif |
| 551 | |
| 552 | /* Save the EL3 system registers needed to return from this exception */ |
| 553 | mrs x0, spsr_el3 |
| 554 | mrs x1, elr_el3 |
| 555 | stp x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] |
| 556 | |
| 557 | /* Switch to the runtime stack i.e. SP_EL0 */ |
| 558 | ldr x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] |
| 559 | mov x20, sp |
| 560 | msr spsel, #MODE_SP_EL0 |
| 561 | mov sp, x2 |
| 562 | |
| 563 | /* |
| 564 | * Find out whether this is a valid interrupt type. |
| 565 | * If the interrupt controller reports a spurious interrupt then return |
| 566 | * to where we came from. |
| 567 | */ |
| 568 | bl plat_ic_get_pending_interrupt_type |
| 569 | cmp x0, #INTR_TYPE_INVAL |
| 570 | b.eq interrupt_exit |
| 571 | |
| 572 | /* |
| 573 | * Get the registered handler for this interrupt type. |
| 574 | * A NULL return value could be 'cause of the following conditions: |
| 575 | * |
| 576 | * a. An interrupt of a type was routed correctly but a handler for its |
| 577 | * type was not registered. |
| 578 | * |
| 579 | * b. An interrupt of a type was not routed correctly so a handler for |
| 580 | * its type was not registered. |
| 581 | * |
| 582 | * c. An interrupt of a type was routed correctly to EL3, but was |
| 583 | * deasserted before its pending state could be read. Another |
| 584 | * interrupt of a different type pended at the same time and its |
| 585 | * type was reported as pending instead. However, a handler for this |
| 586 | * type was not registered. |
| 587 | * |
| 588 | * a. and b. can only happen due to a programming error. The |
| 589 | * occurrence of c. could be beyond the control of Trusted Firmware. |
| 590 | * It makes sense to return from this exception instead of reporting an |
| 591 | * error. |
| 592 | */ |
| 593 | bl get_interrupt_type_handler |
| 594 | cbz x0, interrupt_exit |
| 595 | mov x21, x0 |
| 596 | |
| 597 | mov x0, #INTR_ID_UNAVAILABLE |
| 598 | |
| 599 | /* Set the current security state in the 'flags' parameter */ |
| 600 | mrs x2, scr_el3 |
| 601 | ubfx x1, x2, #0, #1 |
| 602 | |
| 603 | /* Restore the reference to the 'handle' i.e. SP_EL3 */ |
| 604 | mov x2, x20 |
| 605 | |
| 606 | /* x3 will point to a cookie (not used now) */ |
| 607 | mov x3, xzr |
| 608 | |
| 609 | /* Call the interrupt type handler */ |
| 610 | blr x21 |
| 611 | |
| 612 | interrupt_exit: |
| 613 | /* Return from exception, possibly in a different security state */ |
| 614 | b el3_exit |
| 615 | endfunc handle_interrupt_exception |
| 616 | |
Manish Pandey | a4752e2 | 2023-10-11 11:52:24 +0100 | [diff] [blame] | 617 | func imp_def_el3_handler |
| 618 | /* Save GP registers */ |
| 619 | stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] |
| 620 | stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] |
| 621 | stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] |
| 622 | |
| 623 | /* Get the cpu_ops pointer */ |
| 624 | bl get_cpu_ops_ptr |
| 625 | |
| 626 | /* Get the cpu_ops exception handler */ |
| 627 | ldr x0, [x0, #CPU_E_HANDLER_FUNC] |
| 628 | |
| 629 | /* |
| 630 | * If the reserved function pointer is NULL, this CPU does not have an |
| 631 | * implementation defined exception handler function |
| 632 | */ |
| 633 | cbz x0, el3_handler_exit |
| 634 | mrs x1, esr_el3 |
| 635 | ubfx x1, x1, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 636 | blr x0 |
| 637 | el3_handler_exit: |
| 638 | ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] |
| 639 | ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] |
| 640 | ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] |
| 641 | restore_x30 |
| 642 | no_ret report_unhandled_exception |
| 643 | endfunc imp_def_el3_handler |
| 644 | |
| 645 | /* |
| 646 | * Handler for async EA from lower EL synchronized at EL3 entry in KFH mode. |
| 647 | * |
| 648 | * This scenario may arise when there is an error (EA) in the system which is not |
| 649 | * yet signaled to PE while executing in lower EL. During entry into EL3, the errors |
| 650 | * are synchronized either implicitly or explicitly causing async EA to pend at EL3. |
| 651 | * |
| 652 | * On detecting the pending EA (via ISR_EL1.A) and if the EA routing model is |
| 653 | * KFH (SCR_EL3.EA = 1) this handler reflects ther error back to lower EL. |
| 654 | * |
| 655 | * This function assumes x30 has been saved. |
| 656 | */ |
| 657 | func reflect_pending_async_ea_to_lower_el |
| 658 | /* |
| 659 | * As the original exception was not handled we need to ensure that we return |
| 660 | * back to the instruction which caused the exception. To acheive that, eret |
| 661 | * to "elr-4" (Label "subtract_elr_el3") for SMC or simply eret otherwise |
| 662 | * (Label "skip_smc_check"). |
| 663 | * |
| 664 | * LIMITATION: It could be that async EA is masked at the target exception level |
| 665 | * or the priority of async EA wrt to the EL3/secure interrupt is lower, which |
| 666 | * causes back and forth between lower EL and EL3. In case of back and forth between |
| 667 | * lower EL and EL3, we can track the loop count in "CTX_NESTED_EA_FLAG" and leverage |
| 668 | * previous ELR in "CTX_SAVED_ELR_EL3" to detect this cycle and further panic |
| 669 | * to indicate a problem here (Label "check_loop_ctr"). If we are in this cycle, loop |
| 670 | * counter retains its value but if we do a normal el3_exit this flag gets cleared. |
| 671 | * However, setting SCR_EL3.IESB = 1, should give priority to SError handling |
| 672 | * as per AArch64.TakeException pseudo code in Arm ARM. |
| 673 | * |
| 674 | * TODO: In future if EL3 gets a capability to inject a virtual SError to lower |
| 675 | * ELs, we can remove the el3_panic and handle the original exception first and |
| 676 | * inject SError to lower EL before ereting back. |
| 677 | */ |
| 678 | stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] |
| 679 | ldr x29, [sp, #CTX_EL3STATE_OFFSET + CTX_SAVED_ELR_EL3] |
| 680 | mrs x28, elr_el3 |
| 681 | cmp x29, x28 |
| 682 | b.eq check_loop_ctr |
| 683 | str x28, [sp, #CTX_EL3STATE_OFFSET + CTX_SAVED_ELR_EL3] |
| 684 | /* Zero the loop counter */ |
| 685 | str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] |
| 686 | b skip_loop_ctr |
| 687 | check_loop_ctr: |
| 688 | ldr x29, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] |
| 689 | add x29, x29, #1 |
| 690 | str x29, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] |
| 691 | cmp x29, #ASYNC_EA_REPLAY_COUNTER |
| 692 | b.ge el3_panic |
| 693 | skip_loop_ctr: |
| 694 | /* |
| 695 | * Logic to distinguish if we came from SMC or any other exception. |
| 696 | * Use offsets in vector entry to get which exception we are handling. |
| 697 | * In each vector entry of size 0x200, address "0x0-0x80" is for sync |
| 698 | * exception and "0x80-0x200" is for async exceptions. |
| 699 | * Use vector base address (vbar_el3) and exception offset (LR) to |
| 700 | * calculate whether the address we came from is any of the following |
| 701 | * "0x0-0x80", "0x200-0x280", "0x400-0x480" or "0x600-0x680" |
| 702 | */ |
| 703 | mrs x29, vbar_el3 |
| 704 | sub x30, x30, x29 |
| 705 | and x30, x30, #0x1ff |
| 706 | cmp x30, #0x80 |
| 707 | b.ge skip_smc_check |
| 708 | /* Its a synchronous exception, Now check if it is SMC or not? */ |
| 709 | mrs x30, esr_el3 |
| 710 | ubfx x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH |
| 711 | cmp x30, #EC_AARCH32_SMC |
| 712 | b.eq subtract_elr_el3 |
| 713 | cmp x30, #EC_AARCH64_SMC |
| 714 | b.eq subtract_elr_el3 |
| 715 | b skip_smc_check |
| 716 | subtract_elr_el3: |
| 717 | sub x28, x28, #4 |
| 718 | skip_smc_check: |
| 719 | msr elr_el3, x28 |
| 720 | ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] |
| 721 | ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] |
| 722 | exception_return |
| 723 | endfunc reflect_pending_async_ea_to_lower_el |
| 724 | |
Manish Pandey | 62040f4 | 2023-07-20 14:08:38 +0100 | [diff] [blame] | 725 | /* --------------------------------------------------------------------- |
Justin Chadwell | 83e0488 | 2019-08-20 11:01:52 +0100 | [diff] [blame] | 726 | * The following code handles exceptions caused by BRK instructions. |
| 727 | * Following a BRK instruction, the only real valid cause of action is |
| 728 | * to print some information and panic, as the code that caused it is |
| 729 | * likely in an inconsistent internal state. |
| 730 | * |
| 731 | * This is initially intended to be used in conjunction with |
| 732 | * __builtin_trap. |
| 733 | * --------------------------------------------------------------------- |
| 734 | */ |
| 735 | #ifdef MONITOR_TRAPS |
| 736 | func brk_handler |
| 737 | /* Extract the ISS */ |
| 738 | mrs x10, esr_el3 |
| 739 | ubfx x10, x10, #ESR_ISS_SHIFT, #ESR_ISS_LENGTH |
| 740 | |
| 741 | /* Ensure the console is initialized */ |
| 742 | bl plat_crash_console_init |
| 743 | |
| 744 | adr x4, brk_location |
| 745 | bl asm_print_str |
| 746 | mrs x4, elr_el3 |
| 747 | bl asm_print_hex |
| 748 | bl asm_print_newline |
| 749 | |
| 750 | adr x4, brk_message |
| 751 | bl asm_print_str |
| 752 | mov x4, x10 |
| 753 | mov x5, #28 |
| 754 | bl asm_print_hex_bits |
| 755 | bl asm_print_newline |
| 756 | |
| 757 | no_ret plat_panic_handler |
| 758 | endfunc brk_handler |
| 759 | #endif /* MONITOR_TRAPS */ |