rustsbi/rustsbi-qemu

A proper way to set pmp registers

duskmoon314 opened this issue · 3 comments

Now rustsbi-qemu doesn't allow S mode software use devices provided by qemu because we havn't set pmp registers for them.

Noticed that qemu provides more than one board, and they have different MMIO. We need to discuss how to set pmp registers properly.

现在 rustsbi-qemu 不允许 S 态软件使用 qemu 提供的设备,因为我们没有为它们设置 pmp 寄存器。

注意到 qemu 提供了不止一种板子,而且它们有不同的 MMIO。我们需要讨论如何正确设置 pmp 寄存器。


If anyone who wonder how to use devices in qemu, here is an example of using uart0 (uart0_mmio base 0x10000000 lenth 0x100) in virt machine:

如果任何人想知道如何使用 qemu 的外设,下面是使用 virt 的 uart0 (uart0_mmio 基址 0x10000000 长度 0x100) 的一个例子:

fn set_pmp() {
    unsafe {
        asm!(
-            "li     {tmp}, ((0x08 << 16) | (0x1F << 8) | (0x1F << 0))", // 0 = NAPOT,ARWX; 1 = NAPOT,ARWX; 2 = TOR,A;
+            "li     {tmp}, ((0x08 << 24) | (0x1F << 16) | (0x1F << 8) | (0x1F << 0))", // 0 = NAPOT,ARWX; 1 = NAPOT,ARWX; 2 = TOR,A;
            "csrw   0x3A0, {tmp}",
            "li     {tmp}, ((0x0000000080000000 >> 2) | 0x3ffff)", // 0 = 0x0000000080000000-0x000000008001ffff
            "csrw   0x3B0, {tmp}",
            "li     {tmp}, ((0x0000000080200000 >> 2) | 0x1fffff)", // 1 = 0x0000000080200000-0x000000008021ffff
            "csrw   0x3B1, {tmp}",
+            "li     {tmp}, ((0x0000000010000000 >> 2) | 0x3f)",
+            "csrw   0x3B2, {tmp}",
            "sfence.vma",
            tmp = out(reg) _
        )
    };
}

Yes, a proper method to implement this feature is to design a set of macros and structures to handle it thoroughly. A complete API set would be (for example):

rustsbi::pmp! {
    0: (0x80000000, 0x8fffffff) => rwx;
    1: (0x10001000, 0x10001fff) => rw;
    2: tor => _;
}

It requires to add a new macro into rustsbi crate. Thanks for mention!

最好的方法是解析a1(opaque)寄存器的内容,也就是解释设备树,然后根据树的内容去分配pmp的值。opensbi是这样做的:https://github.com/riscv-software-src/opensbi/blob/bd355213bfbb209c047e8cc0df56936f6705477f/lib/utils/fdt/fdt_domain.c#L436。
因为qemu平台的a1可能性很多,它可以由qemu外面的软件去配置,只按固定的地址分配空间实用性就比较局促了。目前临时的做法可以去掉pmp限制,或者按照自己的需求去硬编码。

done