html购物网站模板,社交网站先做pc站可以吗,DW做旅游网站毕业设计模板,黄页网站系统在riscv上电时#xff0c;会进行CPU自检#xff0c;然后跳转到bootloader处执行。bootloader设置好kernel的运行环境后#xff0c;从硬盘加载kernel到内存#xff0c;最后再跳转到kernel入口地址。我们采用的bootloader为OpenSBI#xff0c;被加载到0x80000000地址#x…在riscv上电时会进行CPU自检然后跳转到bootloader处执行。bootloader设置好kernel的运行环境后从硬盘加载kernel到内存最后再跳转到kernel入口地址。我们采用的bootloader为OpenSBI被加载到0x80000000地址OpenSBI探测好外设并初始化内核的环境变量后加载内核到0x80200000地址最后再跳转到0x80200000地址。从上文我们知道我们的入口点_start地址正好为0x80200000也就是OpenSBI会调用我们kernel的_start函数。在riscv64中有如下几种特权级User模式该特权模式为权限最小的模式在linux系统中用户态就运行在该特权级Supervisor模式该特权级时linux操作系统运行的模式特权级别比User模式高Machine模式CPU上电启动后运行在该特权模式该特权比Supervisor更高。从 U 到 S 再到 M权限不断提高这意味着你可以使用更多的特权指令访需求权限更高的寄存器等等。我们可以使用一些指令来修改 CPU 的当前特权级。riscv64 的 M ModeM-mode(机器模式缩写为 M 模式)是 RISC-V 中 hart(hardware thread,硬件线程)可以执行的最高权
限模式。在 M 模式下运行的 hart 对内存,I/O 和一些对于启动和配置系统来说必要的底层功能有着完全的
使用权。riscv64 的 S ModeS-mode(监管者模式缩写为 S 模式)是支持现代类 Unix 操作系统的权限模式支持现代类 Unix 操作系
统所需要的基于页面的虚拟内存机制是其核心。OpenSBI运行在Machine模式当跳转到kernel地址0x80200000执行时会切换到Supervisor模式执行。在我们之前的 _start() 代码中只执行了loop也就是无限循环没有做任何实质的操作。现在我们需要添加不同的功能因此就需要设置一下kernel内核的运行环境。这个运行环境我们首先需要设置的是内核堆栈在rust函数调用时会使用到这个堆栈如果我们不设置sp的地址那么就可能使用sp指向的任何地址这将给程序带来意想不到的后果。由于riscv64的sp地址不能通过rust语言设置因此这部分的环境变量就需要在汇编程序下设置# src/boot/entry_riscv64.asm.section .text.entry.global _start
_start:la sp, bootstacktopcall rust_main.section .bss.stack.align 12.global bootstack
bootstack:.space 4096 * 4.global bootstacktop
bootstacktop:_start 程序放在 .text.entry 这个段中我们链接脚本中将 .text.entry 放在了.text 段的第一个位置也就是将_start函数放在了.text的第一个位置。在_start开始处将堆栈的顶部地址加载到sp寄存器中并且堆栈的大小为16k然后调用rust_main函数。在main.rs中我们将_start函数删除并且添加rust_main函数。#![no_main]
#![no_std]use core::panic::PanicInfo;#[panic_handler]
fn panic(_info: PanicInfo) - ! {loop{}
}#[no_mangle]
extern C fn rust_main() - ! {loop{}
}
为了支持汇编代码我们需要开启global_asm特性因此我们在main.rs中开启该特性并包含src/boot/entry_riscv64.asm汇编代码#![no_main]
#![no_std]use core::panic::PanicInfo;#[panic_handler]
fn panic(_info: PanicInfo) - ! {loop{}
}#![feature(global_asm)]
global_asm!(include_str!(boot/entry_riscv64.asm));#[no_mangle]
extern C fn rust_main() - ! {loop{}
} 注意上面中我们将汇编特性代码添加在panic_handler下面此时会报错:error: an inner attribute is not permitted in this context-- src/main.rs:11:1|
11 | #![feature(global_asm)]| ^^^^^^^^^^^^^^^^^^^^^^^| note: inner attributes, like #![no_std], annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like #[test], annotate the item following them.error[E0658]: use of unstable library feature global_asm: global_asm! is not stable enough for use and is subject to change-- src/main.rs:13:1|
13 | global_asm!(include_str!(boot/entry_riscv64.asm));| ^^^^^^^^^^| note: see issue #35119 https://github.com/rust-lang/rust/issues/35119 for more information help: add #![feature(global_asm)] to the crate attributes to enableerror: aborting due to 2 previous errorsFor more information about this error, try rustc --explain E0658.
error: could not compile rust_os.这里显示错误的原因为global_asm是不稳定的功能需要使用nightly来编译程序。但我们此时已经是使用nightly了。正真的原因是#![feature(global_asm)]特性代码放在了panic_handler代码的后面解决的办法是#![feature(global_asm)]特性必须放在文件开头的位置修改后的代码如下#![no_main]
#![no_std]
#![feature(global_asm)]use core::panic::PanicInfo;#[panic_handler]
fn panic(_info: PanicInfo) - ! {loop{}
}global_asm!(include_str!(boot/entry_riscv64.asm));#[no_mangle]
extern C fn rust_main() - ! {loop{}
} 此时可以编译通过了。