blob: c16e637ad1295cec9e68b0d5688dce5e5793ed5a [file] [log] [blame]
Andrew Walbrandfb73372022-04-21 10:52:27 +00001// 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 Wanga9fe1fb2023-07-04 09:10:35 +000017use vmbase::{
18 eprintln,
Pierre-Clément Tosi1d012772024-10-30 19:20:14 +000019 exceptions::{handle_permission_fault, handle_translation_fault},
Alice Wanga9fe1fb2023-07-04 09:10:35 +000020 exceptions::{ArmException, Esr, HandleExceptionError},
21 logger,
Alice Wanga9fe1fb2023-07-04 09:10:35 +000022 power::reboot,
23 read_sysreg,
24};
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +010025
Alice Wangae9ed2a2023-07-05 11:36:35 +000026fn handle_exception(exception: &ArmException) -> Result<(), HandleExceptionError> {
Jakob Vukalovicb99905d2023-04-20 15:46:02 +010027 // Handle all translation faults on both read and write, and MMIO guard map
28 // flagged invalid pages or blocks that caused the exception.
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +010029 // Handle permission faults for DBM flagged entries, and flag them as dirty on write.
Alice Wangae9ed2a2023-07-05 11:36:35 +000030 match exception.esr {
31 Esr::DataAbortTranslationFault => handle_translation_fault(exception.far),
32 Esr::DataAbortPermissionFault => handle_permission_fault(exception.far),
Jakob Vukalovicb99905d2023-04-20 15:46:02 +010033 _ => Err(HandleExceptionError::UnknownException),
34 }
Jakob Vukalovicd3fe9ed2023-04-20 15:43:09 +010035}
36
Andrew Walbrandfb73372022-04-21 10:52:27 +000037#[no_mangle]
Pierre-Clément Tosieeb1ace2023-05-15 17:23:51 +000038extern "C" fn sync_exception_current(elr: u64, _spsr: u64) {
Jakob Vukalovicd3fe9ed2023-04-20 15:43:09 +010039 // Disable logging in exception handler to prevent unsafe writes to UART.
40 let _guard = logger::suppress();
Jakob Vukalovicd3fe9ed2023-04-20 15:43:09 +010041
Alice Wangae9ed2a2023-07-05 11:36:35 +000042 let exception = ArmException::from_el1_regs();
43 if let Err(e) = handle_exception(&exception) {
44 exception.print("sync_exception_current", e, elr);
Jakob Vukalovicd3fe9ed2023-04-20 15:43:09 +010045 reboot()
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010046 }
Andrew Walbrandfb73372022-04-21 10:52:27 +000047}
48
49#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010050extern "C" fn irq_current(_elr: u64, _spsr: u64) {
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010051 eprintln!("irq_current");
Andrew Walbrandd74b902022-04-14 16:12:50 +000052 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000053}
54
55#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010056extern "C" fn fiq_current(_elr: u64, _spsr: u64) {
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010057 eprintln!("fiq_current");
Andrew Walbrandd74b902022-04-14 16:12:50 +000058 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000059}
60
61#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010062extern "C" fn serr_current(_elr: u64, _spsr: u64) {
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000063 let esr = read_sysreg!("esr_el1");
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010064 eprintln!("serr_current");
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000065 eprintln!("esr={esr:#08x}");
Andrew Walbrandd74b902022-04-14 16:12:50 +000066 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000067}
68
69#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010070extern "C" fn sync_lower(_elr: u64, _spsr: u64) {
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000071 let esr = read_sysreg!("esr_el1");
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010072 eprintln!("sync_lower");
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000073 eprintln!("esr={esr:#08x}");
Andrew Walbrandd74b902022-04-14 16:12:50 +000074 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000075}
76
77#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010078extern "C" fn irq_lower(_elr: u64, _spsr: u64) {
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010079 eprintln!("irq_lower");
Andrew Walbrandd74b902022-04-14 16:12:50 +000080 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000081}
82
83#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010084extern "C" fn fiq_lower(_elr: u64, _spsr: u64) {
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010085 eprintln!("fiq_lower");
Andrew Walbrandd74b902022-04-14 16:12:50 +000086 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000087}
88
89#[no_mangle]
Pierre-Clément Tosi8cbd4b72022-08-11 13:59:31 +010090extern "C" fn serr_lower(_elr: u64, _spsr: u64) {
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000091 let esr = read_sysreg!("esr_el1");
Jakob Vukalovicbff22e12023-04-18 14:50:47 +010092 eprintln!("serr_lower");
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000093 eprintln!("esr={esr:#08x}");
Andrew Walbrandd74b902022-04-14 16:12:50 +000094 reboot();
Andrew Walbrandfb73372022-04-21 10:52:27 +000095}