Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Exception handlers. |
| 16 | |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 17 | use crate::{helpers::page_4kb_of, read_sysreg}; |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 18 | use core::fmt; |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 19 | use vmbase::console; |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 20 | use vmbase::logger; |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 21 | use vmbase::{eprintln, power::reboot}; |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 22 | |
Pierre-Clément Tosi | 446136e | 2022-10-19 10:10:42 +0100 | [diff] [blame] | 23 | const UART_PAGE: usize = page_4kb_of(console::BASE_ADDRESS); |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 24 | |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 25 | #[derive(Debug)] |
| 26 | enum HandleExceptionError { |
| 27 | UnknownException, |
| 28 | } |
| 29 | |
| 30 | impl fmt::Display for HandleExceptionError { |
| 31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 32 | match self { |
| 33 | Self::UnknownException => write!(f, "An unknown exception occurred, not handled."), |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, PartialEq, Copy, Clone)] |
| 39 | enum Esr { |
| 40 | DataAbortTranslationFault, |
| 41 | DataAbortPermissionFault, |
| 42 | DataAbortSyncExternalAbort, |
| 43 | Unknown(usize), |
| 44 | } |
| 45 | |
| 46 | impl Esr { |
| 47 | const EXT_DABT_32BIT: usize = 0x96000010; |
| 48 | const TRANSL_FAULT_BASE_32BIT: usize = 0x96000004; |
| 49 | const TRANSL_FAULT_ISS_MASK_32BIT: usize = !0x143; |
| 50 | const PERM_FAULT_BASE_32BIT: usize = 0x9600004C; |
| 51 | const PERM_FAULT_ISS_MASK_32BIT: usize = !0x103; |
| 52 | } |
| 53 | |
| 54 | impl From<usize> for Esr { |
| 55 | fn from(esr: usize) -> Self { |
| 56 | if esr == Self::EXT_DABT_32BIT { |
| 57 | Self::DataAbortSyncExternalAbort |
| 58 | } else if esr & Self::TRANSL_FAULT_ISS_MASK_32BIT == Self::TRANSL_FAULT_BASE_32BIT { |
| 59 | Self::DataAbortTranslationFault |
| 60 | } else if esr & Self::PERM_FAULT_ISS_MASK_32BIT == Self::PERM_FAULT_BASE_32BIT { |
| 61 | Self::DataAbortPermissionFault |
| 62 | } else { |
| 63 | Self::Unknown(esr) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl fmt::Display for Esr { |
| 69 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 70 | match self { |
| 71 | Self::DataAbortSyncExternalAbort => write!(f, "Synchronous external abort"), |
| 72 | Self::DataAbortTranslationFault => write!(f, "Translation fault"), |
| 73 | Self::DataAbortPermissionFault => write!(f, "Permission fault"), |
| 74 | Self::Unknown(v) => write!(f, "Unknown exception esr={v:#08x}"), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn handle_exception(_esr: Esr, _far: usize) -> Result<(), HandleExceptionError> { |
| 80 | Err(HandleExceptionError::UnknownException) |
| 81 | } |
| 82 | |
| 83 | #[inline] |
| 84 | fn handling_uart_exception(esr: Esr, far: usize) -> bool { |
| 85 | esr == Esr::DataAbortSyncExternalAbort && page_4kb_of(far) == UART_PAGE |
| 86 | } |
| 87 | |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 88 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 89 | extern "C" fn sync_exception_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 90 | // Disable logging in exception handler to prevent unsafe writes to UART. |
| 91 | let _guard = logger::suppress(); |
| 92 | let esr: Esr = read_sysreg!("esr_el1").into(); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 93 | let far = read_sysreg!("far_el1"); |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 94 | |
| 95 | if let Err(e) = handle_exception(esr, far) { |
| 96 | // Don't print to the UART if we are handling an exception it could raise. |
| 97 | if !handling_uart_exception(esr, far) { |
| 98 | eprintln!("sync_exception_current"); |
| 99 | eprintln!("{e}"); |
| 100 | eprintln!("{esr}, far={far:#08x}"); |
| 101 | } |
| 102 | reboot() |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 103 | } |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 107 | extern "C" fn irq_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 108 | eprintln!("irq_current"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 109 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 113 | extern "C" fn fiq_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 114 | eprintln!("fiq_current"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 115 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 119 | extern "C" fn serr_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 120 | let esr = read_sysreg!("esr_el1"); |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 121 | eprintln!("serr_current"); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 122 | eprintln!("esr={esr:#08x}"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 123 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 127 | extern "C" fn sync_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 128 | let esr = read_sysreg!("esr_el1"); |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 129 | eprintln!("sync_lower"); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 130 | eprintln!("esr={esr:#08x}"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 131 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 135 | extern "C" fn irq_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 136 | eprintln!("irq_lower"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 137 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 141 | extern "C" fn fiq_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 142 | eprintln!("fiq_lower"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 143 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 147 | extern "C" fn serr_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 148 | let esr = read_sysreg!("esr_el1"); |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 149 | eprintln!("serr_lower"); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 150 | eprintln!("esr={esr:#08x}"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 151 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 152 | } |