• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 
handle_exception(exception: &ArmException) -> Result<(), HandleExceptionError>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 }
37 
38 #[no_mangle]
sync_exception_current(elr: u64, _spsr: u64)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     }
48 }
49 
50 #[no_mangle]
irq_current()51 extern "C" fn irq_current() {
52     emergency_write_str("irq_current\n");
53     reboot();
54 }
55 
56 #[no_mangle]
fiq_current()57 extern "C" fn fiq_current() {
58     emergency_write_str("fiq_current\n");
59     reboot();
60 }
61 
62 #[no_mangle]
serr_current()63 extern "C" fn serr_current() {
64     emergency_write_str("serr_current\n");
65     print_esr();
66     reboot();
67 }
68 
69 #[no_mangle]
sync_lower()70 extern "C" fn sync_lower() {
71     emergency_write_str("sync_lower\n");
72     print_esr();
73     reboot();
74 }
75 
76 #[no_mangle]
irq_lower()77 extern "C" fn irq_lower() {
78     emergency_write_str("irq_lower\n");
79     reboot();
80 }
81 
82 #[no_mangle]
fiq_lower()83 extern "C" fn fiq_lower() {
84     emergency_write_str("fiq_lower\n");
85     reboot();
86 }
87 
88 #[no_mangle]
serr_lower()89 extern "C" fn serr_lower() {
90     emergency_write_str("serr_lower\n");
91     print_esr();
92     reboot();
93 }
94 
95 #[inline]
print_esr()96 fn print_esr() {
97     let esr = read_sysreg!("esr_el1");
98     eprintln!("esr={:#08x}", esr);
99 }
100