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