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 | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 17 | use core::fmt; |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 18 | use vmbase::console; |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 19 | use vmbase::logger; |
Alice Wang | 93ee98a | 2023-06-08 08:20:39 +0000 | [diff] [blame] | 20 | use vmbase::memory::{page_4kb_of, MemoryTrackerError, MEMORY}; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 21 | use vmbase::read_sysreg; |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 22 | use vmbase::{eprintln, power::reboot}; |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 23 | |
Pierre-Clément Tosi | 446136e | 2022-10-19 10:10:42 +0100 | [diff] [blame] | 24 | const UART_PAGE: usize = page_4kb_of(console::BASE_ADDRESS); |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 25 | |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 26 | #[derive(Debug)] |
| 27 | enum HandleExceptionError { |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 28 | PageTableUnavailable, |
| 29 | PageTableNotInitialized, |
| 30 | InternalError(MemoryTrackerError), |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 31 | UnknownException, |
| 32 | } |
| 33 | |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 34 | impl From<MemoryTrackerError> for HandleExceptionError { |
| 35 | fn from(other: MemoryTrackerError) -> Self { |
| 36 | Self::InternalError(other) |
| 37 | } |
| 38 | } |
| 39 | |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 40 | impl fmt::Display for HandleExceptionError { |
| 41 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 42 | match self { |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 43 | Self::PageTableUnavailable => write!(f, "Page table is not available."), |
| 44 | Self::PageTableNotInitialized => write!(f, "Page table is not initialized."), |
| 45 | Self::InternalError(e) => write!(f, "Error while updating page table: {e}"), |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 46 | Self::UnknownException => write!(f, "An unknown exception occurred, not handled."), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[derive(Debug, PartialEq, Copy, Clone)] |
| 52 | enum Esr { |
| 53 | DataAbortTranslationFault, |
| 54 | DataAbortPermissionFault, |
| 55 | DataAbortSyncExternalAbort, |
| 56 | Unknown(usize), |
| 57 | } |
| 58 | |
| 59 | impl Esr { |
| 60 | const EXT_DABT_32BIT: usize = 0x96000010; |
| 61 | const TRANSL_FAULT_BASE_32BIT: usize = 0x96000004; |
| 62 | const TRANSL_FAULT_ISS_MASK_32BIT: usize = !0x143; |
| 63 | const PERM_FAULT_BASE_32BIT: usize = 0x9600004C; |
| 64 | const PERM_FAULT_ISS_MASK_32BIT: usize = !0x103; |
| 65 | } |
| 66 | |
| 67 | impl From<usize> for Esr { |
| 68 | fn from(esr: usize) -> Self { |
| 69 | if esr == Self::EXT_DABT_32BIT { |
| 70 | Self::DataAbortSyncExternalAbort |
| 71 | } else if esr & Self::TRANSL_FAULT_ISS_MASK_32BIT == Self::TRANSL_FAULT_BASE_32BIT { |
| 72 | Self::DataAbortTranslationFault |
| 73 | } else if esr & Self::PERM_FAULT_ISS_MASK_32BIT == Self::PERM_FAULT_BASE_32BIT { |
| 74 | Self::DataAbortPermissionFault |
| 75 | } else { |
| 76 | Self::Unknown(esr) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl fmt::Display for Esr { |
| 82 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 83 | match self { |
| 84 | Self::DataAbortSyncExternalAbort => write!(f, "Synchronous external abort"), |
| 85 | Self::DataAbortTranslationFault => write!(f, "Translation fault"), |
| 86 | Self::DataAbortPermissionFault => write!(f, "Permission fault"), |
| 87 | Self::Unknown(v) => write!(f, "Unknown exception esr={v:#08x}"), |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 92 | #[inline] |
| 93 | fn handle_translation_fault(far: usize) -> Result<(), HandleExceptionError> { |
| 94 | let mut guard = MEMORY.try_lock().ok_or(HandleExceptionError::PageTableUnavailable)?; |
| 95 | let memory = guard.as_mut().ok_or(HandleExceptionError::PageTableNotInitialized)?; |
| 96 | Ok(memory.handle_mmio_fault(far)?) |
| 97 | } |
| 98 | |
| 99 | #[inline] |
| 100 | fn handle_permission_fault(far: usize) -> Result<(), HandleExceptionError> { |
| 101 | let mut guard = MEMORY.try_lock().ok_or(HandleExceptionError::PageTableUnavailable)?; |
| 102 | let memory = guard.as_mut().ok_or(HandleExceptionError::PageTableNotInitialized)?; |
| 103 | Ok(memory.handle_permission_fault(far)?) |
| 104 | } |
| 105 | |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 106 | fn handle_exception(esr: Esr, far: usize) -> Result<(), HandleExceptionError> { |
| 107 | // Handle all translation faults on both read and write, and MMIO guard map |
| 108 | // flagged invalid pages or blocks that caused the exception. |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 109 | // Handle permission faults for DBM flagged entries, and flag them as dirty on write. |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 110 | match esr { |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 111 | Esr::DataAbortTranslationFault => handle_translation_fault(far), |
| 112 | Esr::DataAbortPermissionFault => handle_permission_fault(far), |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 113 | _ => Err(HandleExceptionError::UnknownException), |
| 114 | } |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Alice Wang | 8364a48 | 2023-07-05 08:06:05 +0000 | [diff] [blame] | 117 | /// Prints the details of an exception failure, excluding UART exceptions. |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 118 | #[inline] |
Alice Wang | 0ae33e9 | 2023-07-05 11:18:54 +0000 | [diff] [blame] | 119 | fn print_exception_failure( |
| 120 | esr: Esr, |
| 121 | far: usize, |
| 122 | elr: u64, |
| 123 | e: HandleExceptionError, |
| 124 | exception_name: &str, |
| 125 | ) { |
Alice Wang | 8364a48 | 2023-07-05 08:06:05 +0000 | [diff] [blame] | 126 | let is_uart_exception = esr == Esr::DataAbortSyncExternalAbort && page_4kb_of(far) == UART_PAGE; |
| 127 | // Don't print to the UART if we are handling an exception it could raise. |
| 128 | if !is_uart_exception { |
Alice Wang | 0ae33e9 | 2023-07-05 11:18:54 +0000 | [diff] [blame] | 129 | eprintln!("{exception_name}"); |
Alice Wang | 8364a48 | 2023-07-05 08:06:05 +0000 | [diff] [blame] | 130 | eprintln!("{e}"); |
| 131 | eprintln!("{esr}, far={far:#08x}, elr={elr:#08x}"); |
| 132 | } |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 135 | #[no_mangle] |
Pierre-Clément Tosi | eeb1ace | 2023-05-15 17:23:51 +0000 | [diff] [blame] | 136 | extern "C" fn sync_exception_current(elr: u64, _spsr: u64) { |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 137 | // Disable logging in exception handler to prevent unsafe writes to UART. |
| 138 | let _guard = logger::suppress(); |
| 139 | let esr: Esr = read_sysreg!("esr_el1").into(); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 140 | let far = read_sysreg!("far_el1"); |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 141 | |
| 142 | if let Err(e) = handle_exception(esr, far) { |
Alice Wang | 0ae33e9 | 2023-07-05 11:18:54 +0000 | [diff] [blame] | 143 | print_exception_failure(esr, far, elr, e, "sync_exception_current"); |
Jakob Vukalovic | d3fe9ed | 2023-04-20 15:43:09 +0100 | [diff] [blame] | 144 | reboot() |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 145 | } |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 149 | extern "C" fn irq_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 150 | eprintln!("irq_current"); |
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 | } |
| 153 | |
| 154 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 155 | extern "C" fn fiq_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 156 | eprintln!("fiq_current"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 157 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 161 | extern "C" fn serr_current(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 162 | let esr = read_sysreg!("esr_el1"); |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 163 | eprintln!("serr_current"); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 164 | eprintln!("esr={esr:#08x}"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 165 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 169 | extern "C" fn sync_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 170 | let esr = read_sysreg!("esr_el1"); |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 171 | eprintln!("sync_lower"); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 172 | eprintln!("esr={esr:#08x}"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 173 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 177 | extern "C" fn irq_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 178 | eprintln!("irq_lower"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 179 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 183 | extern "C" fn fiq_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 184 | eprintln!("fiq_lower"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 185 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | #[no_mangle] |
Pierre-Clément Tosi | 8cbd4b7 | 2022-08-11 13:59:31 +0100 | [diff] [blame] | 189 | extern "C" fn serr_lower(_elr: u64, _spsr: u64) { |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 190 | let esr = read_sysreg!("esr_el1"); |
Jakob Vukalovic | bff22e1 | 2023-04-18 14:50:47 +0100 | [diff] [blame] | 191 | eprintln!("serr_lower"); |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 192 | eprintln!("esr={esr:#08x}"); |
Andrew Walbran | dd74b90 | 2022-04-14 16:12:50 +0000 | [diff] [blame] | 193 | reboot(); |
Andrew Walbran | dfb7337 | 2022-04-21 10:52:27 +0000 | [diff] [blame] | 194 | } |