< 返回版块

amwps290 发表于 2023-06-28 17:26

.
├── Cargo.lock
├── Cargo.toml
├── last-qemu
├── Makefile
├── src
│   ├── console.rs
│   ├── entry.asm
│   ├── lang_items.rs
│   ├── linker.ld
│   ├── linker-qemu.ld
│   ├── main.rs
│   ├── qemu.rs
│   └── sbi.rs
└── target

我现在在写那个 rcore 的代码,想在 sbi.rs 使用 qemu.rs 的东西,该如何引用呢,现在引用就报错?

qemu.rs

//ref:: https://github.com/andre-richter/qemu-exit
use core::arch::asm;

const EXIT_SUCCESS: u32 = 0x5555; // Equals `exit(0)`. qemu successful exit

const EXIT_FAILURE_FLAG: u32 = 0x3333;
const EXIT_FAILURE: u32 = exit_code_encode(1); // Equals `exit(1)`. qemu failed exit
const EXIT_RESET: u32 = 0x7777; // qemu reset

pub trait QEMUExit {
    /// Exit with specified return code.
    ///
    /// Note: For `X86`, code is binary-OR'ed with `0x1` inside QEMU.
    fn exit(&self, code: u32) -> !;

    /// Exit QEMU using `EXIT_SUCCESS`, aka `0`, if possible.
    ///
    /// Note: Not possible for `X86`.
    fn exit_success(&self) -> !;

    /// Exit QEMU using `EXIT_FAILURE`, aka `1`.
    fn exit_failure(&self) -> !;
}

/// RISCV64 configuration
pub struct RISCV64 {
    /// Address of the sifive_test mapped device.
    addr: u64,
}

/// Encode the exit code using EXIT_FAILURE_FLAG.
const fn exit_code_encode(code: u32) -> u32 {
    (code << 16) | EXIT_FAILURE_FLAG
}

impl RISCV64 {
    /// Create an instance.
    pub const fn new(addr: u64) -> Self {
        RISCV64 { addr }
    }
}

impl QEMUExit for RISCV64 {
    /// Exit qemu with specified exit code.
    fn exit(&self, code: u32) -> ! {
        // If code is not a special value, we need to encode it with EXIT_FAILURE_FLAG.
        let code_new = match code {
            EXIT_SUCCESS | EXIT_FAILURE | EXIT_RESET => code,
            _ => exit_code_encode(code),
        };

        unsafe {
            asm!(
                "sw {0}, 0({1})",
                in(reg)code_new, in(reg)self.addr
            );

            // For the case that the QEMU exit attempt did not work, transition into an infinite
            // loop. Calling `panic!()` here is unfeasible, since there is a good chance
            // this function here is the last expression in the `panic!()` handler
            // itself. This prevents a possible infinite loop.
            loop {
                asm!("wfi", options(nomem, nostack));
            }
        }
    }

    fn exit_success(&self) -> ! {
        self.exit(EXIT_SUCCESS);
    }

    fn exit_failure(&self) -> ! {
        self.exit(EXIT_FAILURE);
    }
}

const VIRT_TEST: u64 = 0x100000;

pub const QEMU_EXIT_HANDLE: RISCV64 = RISCV64::new(VIRT_TEST);

sbi.rs

#![allow(unused)]

use core::arch::asm;
const SBI_SET_TIMER: usize = 0;
const SBI_CONSOLE_PUTCHAR: usize = 1;
const SBI_CONSOLE_GETCHAR: usize = 2;
const SBI_CLEAR_IPI: usize = 3;
const SBI_SEND_IPI: usize = 4;
const SBI_REMOTE_FENCE_I: usize = 5;
const SBI_REMOTE_SFENCE_VMA: usize = 6;
const SBI_REMOTE_SFENCE_VMA_ASID: usize = 7;
const SBI_SHUTDOWN: usize = 8;



#[inline(always)]

fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
    let mut ret;
    unsafe {
        asm!(
            "li x16, 0",
            "ecall",
            inlateout("x10") arg0 => ret,
            in("x11") arg1,
            in("x12") arg2,
            in("x17") which,
        )
    }

    ret
}

pub fn console_putchar(c: usize) {
    sbi_call(SBI_CONSOLE_PUTCHAR, c, 0, 0);
}

pub fn console_getchar() -> usize{
    sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0)
}

pub fn shutdown() -> ! {
    // sbi_call(SBI_SHUTDOWN, 0, 0, 0);
    qemu::QEMU_EXIT_HANDLE.exit_failure();
    panic!("It should shutdown!");
}

在最后的 shutdown 函数中使用了 qemu 的相关内容

评论区

写评论
itfanr 2023-07-04 09:23

一般是 main.rs 独自在一层,然后 lib.rs 或者 mod.rs 管理其他的 rust 代码。

small-white0-0 2023-06-28 20:05

我猜 你是把 mod sbi;mod qume;是放在main.rs的。这种情况下,qume.rs不是sbi.rs的父子模块,在sbi中是不可见qume这个名字的。 你可以改为绝对模块路径,没记错的话是:crate::qemu::QEMU_EXIT_HANDLE.exit_failure();或者sbi.rs在开头加上use crate::qemu;

不过,这个crate好像是意味着lib.rs。如果使用绝对路径还不行,或许你还需要新建一个“lib.rs“,并将mod sbi;mod qume;放到”lib.rs“文件中(应该是不用的,不太记得清了。)你可以看看 https://course.rs/basic/crate-module/module.html?highlight=crate#%E7%94%A8%E8%B7%AF%E5%BE%84%E5%BC%95%E7%94%A8%E6%A8%A1%E5%9D%97

HC97 2023-06-28 19:53
1 共 3 条评论, 1 页