qemu_rv32_virt/
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::str;
8
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        let uart = qemu_rv32_virt_chip::uart::Uart16550::new(qemu_rv32_virt_chip::uart::UART0_BASE);
30        uart.transmit_sync(buf);
31        buf.len()
32    }
33}
34
35/// Panic handler.
36#[cfg(not(test))]
37#[no_mangle]
38#[panic_handler]
39pub unsafe fn panic_fmt(pi: &PanicInfo) -> ! {
40    use core::ptr::{addr_of, addr_of_mut};
41
42    let writer = &mut *addr_of_mut!(WRITER);
43
44    debug::panic_print::<_, _, _>(
45        writer,
46        pi,
47        &rv32i::support::nop,
48        &*addr_of!(PROCESSES),
49        &*addr_of!(CHIP),
50        &*addr_of!(PROCESS_PRINTER),
51    );
52
53    // The system is no longer in a well-defined state. Use
54    // semihosting commands to exit QEMU with a return code of 1.
55    rv32i::semihost_command(0x18, 1, 0);
56
57    // To satisfy the ! return type constraints.
58    loop {}
59}