Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify print macro to use sbi calling #70

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions src/device/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::{MmioDevice, PTE_FLAGS_FOR_DEVICE};
use crate::memmap::{GuestPhysicalAddress, HostPhysicalAddress, MemoryMap};

use core::cell::OnceCell;
use core::fmt::{self, Write};
use fdt::Fdt;
use rustsbi::{Physical, SbiRet};
use spin::Mutex;
Expand All @@ -16,46 +15,9 @@ mod register {
pub const LSR_OFFSET: usize = 3;
}

/// Print to standard output.
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::device::uart::_print(format_args!($($arg)*)));
}

/// Print with linebreak to standard output.
#[macro_export]
macro_rules! println {
($fmt:expr) => (print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
}

/// Print function calling from print macro
pub fn _print(args: fmt::Arguments) {
let mut writer = UartWriter {};
writer.write_fmt(args).unwrap();
}

/// Uart address for `UartWriter`.
static UART_ADDR: Mutex<OnceCell<HostPhysicalAddress>> = Mutex::new(OnceCell::new());

/// Struct for `Write` trait.
struct UartWriter;

impl Write for UartWriter {
/// Write string to tty via UART.
#[allow(clippy::cast_possible_wrap)]
fn write_str(&mut self, s: &str) -> fmt::Result {
let uart_addr = UART_ADDR.lock().get().unwrap().raw() as *mut u32;
for c in s.bytes() {
unsafe {
while (uart_addr.read_volatile() as i32) < 0 {}
uart_addr.write_volatile(u32::from(c));
}
}
Ok(())
}
}

/// UART: Universal asynchronous receiver-transmitter
#[derive(Debug)]
pub struct Uart {
Expand Down
2 changes: 2 additions & 0 deletions src/hypervisor_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use riscv::register::{sepc, sie, sscratch, sstatus, sstatus::FS, stvec};
/// Entry point to HS-mode.
#[inline(never)]
pub extern "C" fn hstart(hart_id: usize, dtb_addr: usize) -> ! {
crate::println!("welcome to hikami");

// hart_id must be zero.
assert_eq!(hart_id, 0);

Expand Down
36 changes: 36 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Print macros for logging

use core::fmt::{self, Write};
use sbi_rt::Physical;

/// Writer for print macro.
struct Writer;
impl core::fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result {
sbi_rt::console_write(Physical::new(
s.len(),
s.as_ptr() as usize & 0xffff_ffff,
(s.as_ptr() as usize >> 32) & 0xffff_ffff,
));
Ok(())
}
}

/// Print function calling from print macro
pub fn print_for_macro(args: fmt::Arguments) {
let mut writer = Writer;
writer.write_fmt(args).unwrap();
}

/// Print to standard output.
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::log::print_for_macro(format_args!($($arg)*)));
}

/// Print with linebreak to standard output.
#[macro_export]
macro_rules! println {
($fmt:expr) => ($crate::print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::print!(concat!($fmt, "\n"), $($arg)*));
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#![no_std]
// TODO: remove nightly when `naked_functions` become stable.
#![feature(naked_functions)]
// TODO: FIX AND REMOVE IT!!!
#![allow(static_mut_refs)]

extern crate alloc;
mod device;
mod emulate_extension;
mod guest;
mod h_extension;
mod hypervisor_init;
mod log;
mod memmap;
mod trap;

Expand Down
Loading