David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [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 | |
Alice Wang | f1d5efd | 2023-07-04 15:07:59 +0000 | [diff] [blame^] | 17 | use vmbase::{ |
| 18 | console::emergency_write_str, |
| 19 | eprintln, |
| 20 | exceptions::{ArmException, Esr, HandleExceptionError}, |
| 21 | logger, |
| 22 | memory::{handle_permission_fault, handle_translation_fault}, |
| 23 | power::reboot, |
| 24 | read_sysreg, |
| 25 | }; |
| 26 | |
| 27 | fn handle_exception(exception: &ArmException) -> Result<(), HandleExceptionError> { |
| 28 | // Handle all translation faults on both read and write, and MMIO guard map |
| 29 | // flagged invalid pages or blocks that caused the exception. |
| 30 | // Handle permission faults for DBM flagged entries, and flag them as dirty on write. |
| 31 | match exception.esr { |
| 32 | Esr::DataAbortTranslationFault => handle_translation_fault(exception.far), |
| 33 | Esr::DataAbortPermissionFault => handle_permission_fault(exception.far), |
| 34 | _ => Err(HandleExceptionError::UnknownException), |
| 35 | } |
| 36 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 37 | |
| 38 | #[no_mangle] |
Alice Wang | f1d5efd | 2023-07-04 15:07:59 +0000 | [diff] [blame^] | 39 | extern "C" fn sync_exception_current(elr: u64, _spsr: u64) { |
| 40 | // Disable logging in exception handler to prevent unsafe writes to UART. |
| 41 | let _guard = logger::suppress(); |
| 42 | |
| 43 | let exception = ArmException::from_el1_regs(); |
| 44 | if let Err(e) = handle_exception(&exception) { |
| 45 | exception.print("sync_exception_current", e, elr); |
| 46 | reboot() |
| 47 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | #[no_mangle] |
| 51 | extern "C" fn irq_current() { |
| 52 | emergency_write_str("irq_current\n"); |
| 53 | reboot(); |
| 54 | } |
| 55 | |
| 56 | #[no_mangle] |
| 57 | extern "C" fn fiq_current() { |
| 58 | emergency_write_str("fiq_current\n"); |
| 59 | reboot(); |
| 60 | } |
| 61 | |
| 62 | #[no_mangle] |
| 63 | extern "C" fn serr_current() { |
| 64 | emergency_write_str("serr_current\n"); |
| 65 | print_esr(); |
| 66 | reboot(); |
| 67 | } |
| 68 | |
| 69 | #[no_mangle] |
| 70 | extern "C" fn sync_lower() { |
| 71 | emergency_write_str("sync_lower\n"); |
| 72 | print_esr(); |
| 73 | reboot(); |
| 74 | } |
| 75 | |
| 76 | #[no_mangle] |
| 77 | extern "C" fn irq_lower() { |
| 78 | emergency_write_str("irq_lower\n"); |
| 79 | reboot(); |
| 80 | } |
| 81 | |
| 82 | #[no_mangle] |
| 83 | extern "C" fn fiq_lower() { |
| 84 | emergency_write_str("fiq_lower\n"); |
| 85 | reboot(); |
| 86 | } |
| 87 | |
| 88 | #[no_mangle] |
| 89 | extern "C" fn serr_lower() { |
| 90 | emergency_write_str("serr_lower\n"); |
| 91 | print_esr(); |
| 92 | reboot(); |
| 93 | } |
| 94 | |
| 95 | #[inline] |
| 96 | fn print_esr() { |
Pierre-Clément Tosi | 9a7328e | 2023-07-07 15:51:51 +0000 | [diff] [blame] | 97 | let esr = read_sysreg!("esr_el1"); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 98 | eprintln!("esr={:#08x}", esr); |
| 99 | } |