veer_el2_sim/
io.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5use core::fmt::Write;
6use core::panic::PanicInfo;
7use core::ptr::addr_of_mut;
8use core::ptr::write_volatile;
9use kernel::debug;
10use kernel::debug::IoWrite;
11
12struct Writer {}
13
14static mut WRITER: Writer = Writer {};
15
16impl Write for Writer {
17    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
18        self.write(s.as_bytes());
19        Ok(())
20    }
21}
22
23impl IoWrite for Writer {
24    fn write(&mut self, buf: &[u8]) -> usize {
25        for b in buf {
26            // Print to a special address for simulation output
27            unsafe {
28                write_volatile(0xd0580000 as *mut u32, (*b) as u32);
29            }
30        }
31        buf.len()
32    }
33}
34
35/// Panic handler.
36///
37/// # Safety
38/// Accesses memory-mapped registers.
39#[cfg(not(test))]
40#[panic_handler]
41pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
42    let writer = &mut *addr_of_mut!(WRITER);
43
44    debug::panic_print(
45        writer,
46        pi,
47        &rv32i::support::nop,
48        crate::PANIC_RESOURCES.get(),
49    );
50
51    // By writing 0xff to this address we can exit the simulation.
52    // So instead of blinking in a loop let's exit the simulation.
53    write_volatile(0xd0580000 as *mut u8, 0xff);
54
55    unreachable!()
56}