|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | +// |
| 3 | +// Copyright (c) 2024 Philipp Schulz <[email protected]> |
| 4 | + |
| 5 | +//! AArch32. |
| 6 | +
|
| 7 | +use crate::QEMUExit; |
| 8 | +use core::arch::asm; |
| 9 | + |
| 10 | +const EXIT_SUCCESS: u32 = 0; |
| 11 | +const EXIT_FAILURE: u32 = 1; |
| 12 | + |
| 13 | +#[allow(non_upper_case_globals)] |
| 14 | +const ADP_Stopped_ApplicationExit: u32 = 0x20026; |
| 15 | + |
| 16 | +/// The parameter block layout that is expected by QEMU. |
| 17 | +/// |
| 18 | +/// If QEMU finds `ADP_Stopped_ApplicationExit` in the first parameter, it uses the second parameter |
| 19 | +/// as exit code. |
| 20 | +/// |
| 21 | +/// If first paraemter != `ADP_Stopped_ApplicationExit`, exit code `1` is used. |
| 22 | +#[repr(C)] |
| 23 | +struct QEMUParameterBlock { |
| 24 | + arg0: u32, |
| 25 | + arg1: u32, |
| 26 | +} |
| 27 | + |
| 28 | +/// AArch32 configuration. |
| 29 | +pub struct AArch32 {} |
| 30 | + |
| 31 | +/// A Semihosting call using `0x20` - `SYS_EXIT_EXTENDED`. |
| 32 | +fn semihosting_sys_exit_call(block: &QEMUParameterBlock) -> ! { |
| 33 | + unsafe { |
| 34 | + asm!( |
| 35 | + "bkpt #0xab", |
| 36 | + in("r0") 0x20, |
| 37 | + in("r1") block as *const _ as u32, |
| 38 | + options(nostack) |
| 39 | + ); |
| 40 | + |
| 41 | + // For the case that the QEMU exit attempt did not work, transition into an infinite loop. |
| 42 | + // Calling `panic!()` here is unfeasible, since there is a good chance this function here is |
| 43 | + // the last expression in the `panic!()` handler itself. This prevents a possible |
| 44 | + // infinite loop. |
| 45 | + loop { |
| 46 | + asm!("wfe", options(nomem, nostack)); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl AArch32 { |
| 52 | + /// Create an instance. |
| 53 | + pub const fn new() -> Self { |
| 54 | + AArch32 {} |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl QEMUExit for AArch32 { |
| 59 | + fn exit(&self, code: u32) -> ! { |
| 60 | + let block = QEMUParameterBlock { |
| 61 | + arg0: ADP_Stopped_ApplicationExit, |
| 62 | + arg1: code as u32, |
| 63 | + }; |
| 64 | + |
| 65 | + semihosting_sys_exit_call(&block) |
| 66 | + } |
| 67 | + |
| 68 | + fn exit_success(&self) -> ! { |
| 69 | + self.exit(EXIT_SUCCESS) |
| 70 | + } |
| 71 | + |
| 72 | + fn exit_failure(&self) -> ! { |
| 73 | + self.exit(EXIT_FAILURE) |
| 74 | + } |
| 75 | +} |
0 commit comments