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::write_volatile;
8use core::ptr::{addr_of, addr_of_mut};
9use kernel::debug;
10use kernel::debug::IoWrite;
11
12use crate::CHIP;
13use crate::PROCESSES;
14use crate::PROCESS_PRINTER;
15
16struct Writer {}
17
18static mut WRITER: Writer = Writer {};
19
20impl Write for Writer {
21    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
22        self.write(s.as_bytes());
23        Ok(())
24    }
25}
26
27impl IoWrite for Writer {
28    fn write(&mut self, buf: &[u8]) -> usize {
29        for b in buf {
30            // Print to a special address for simulation output
31            unsafe {
32                write_volatile(0xd0580000 as *mut u32, (*b) as u32);
33            }
34        }
35        buf.len()
36    }
37}
38
39/// Panic handler.
40///
41/// # Safety
42/// Accesses memory-mapped registers.
43#[cfg(not(test))]
44#[no_mangle]
45#[panic_handler]
46pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
47    let writer = &mut *addr_of_mut!(WRITER);
48
49    debug::panic_print(
50        writer,
51        pi,
52        &rv32i::support::nop,
53        &*addr_of!(PROCESSES),
54        &*addr_of!(CHIP),
55        &*addr_of!(PROCESS_PRINTER),
56    );
57
58    // By writing 0xff to this address we can exit the simulation.
59    // So instead of blinking in a loop let's exit the simulation.
60    write_volatile(0xd0580000 as *mut u8, 0xff);
61
62    unreachable!()
63}